Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Archiving logs created by Proxy+ http proxy server

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Paul Purvis
    replied
    In order for the program above proxypluslogrotate to do a good job, there needs to be a way for a limited user to stop and restart the proxyplus proxy server service.

    A program from microsoft will let you do that.
    Get a copy of the program subinacl.exe located in the file subinacl.msi currently from the microsoft.com website.
    Execute the comand below from the command prompt while logged in as an administrator user(accout).

    This appears to be working for me. If you want to be specific about a certain limited user, then replace the word everyone for the user's name.

    subinacl.exe /service "ProxyPlus" /grant=everyone=to

    Now when you run the program proxypluslogrotate, usually at bootup, the program will stop the service in order to capture the current log files and if the service was started when this program is run, the program will restart the service before it ends. So thereby, you should not have to worry about the commands "net stop ProxyPlus" and/or "net start ProxyPlus".

    Again with these enhancements, you could actually not have to configure proxyplus to do any file rotations. This should take of those events. Only if you are not rebooting a computer often will you have to consider setting the configuration to do rotating of log files.

    I am so glad this is hopefully done.

    Leave a comment:


  • Paul Purvis
    replied
    here is the program to rotate the log files in proxyplus.
    proxy plus will only rotate files up to 31 in number.
    this program does not care, it will probably fill your hard drive before it complains. ok it is 100,000 log file rotations.
    Once again, other than the current log file, the log file with the lowest number in it is the most current by the program proxyplus proxy server and so is the same in this program also.
    This program is need, because the proxyplus program will only turn log files at midnight.

    This program will just rotate them, and it will not create a current log file, this program leaves it up to the proxyplus server to create a new current log file.

    If the proxyplus proxy server is currently running as a service, this program try to stop it, before rotating log files. If the service is unable to be stopped, this program is supposed to abort out.
    If this program was able to stop the service if it was running, it will rotate the logs and try to start the proxyplus proxy server service back up after rotating files.

    I plan on running the proxy server on every computer rather than on a central server, for reasons of i do not want to be bothered about a computer going down and stopping all the other computers from accessing the internet. I am only worried about at this time is web url logging and supervision.

    with this program you can also turn off the auto rotate log files in proxyplus and do your own rotating manually with this program. i have no idea how big a log file can become and whether there are limititations or not.


    Code:
    'proxpluslogrotate.bas
    'compiled with powerbasic pbcc50
    '
    'this program rotates the all log files of proxyplus proxy server
    'if the proxyplus service will be stopped before rotating logs and rerestarted after
    '  the proxyplus serivice keeps the current logs in a opened condition the entire time
    '  it is running as a service.
    '
    '  you have to have privleges to start and stop a service.
    '  if the service is found running and cannot be stopped , this program will abort
    '
    '  Peter Redei    wrote the function isservicerunning function
    '  Peter Lameijn  wrote the function servicestop function
    '  William Burns  wrote the function startmanually function
    '  and headers to forums are given.
    '
    '
    #COMPILE EXE  "proxypluslogrotate.exe"
    #DIM ALL
    #REGISTER NONE
    #INCLUDE "WIN32API.INC"
    
    
    'http://www.powerbasic.com/support/forums/Forum7/HTML/001418.html
    '----------------------------------------------------------------------------------------------------------------------------------------
    ' this code is based on Microsoft MSKB Q183478
    '----------------------------------------------------------------------------------------------------------------------------------------
    FUNCTION IsServiceRunning(lngServiceType AS LONG, BYVAL strText AS STRING, lstServiceName AS LONG, lstDisplayName AS LONG) AS LONG
        LOCAL hSCManager                    AS DWORD
        LOCAL x                             AS LONG
        LOCAL dx                            AS ENUM_SERVICE_STATUS
        LOCAL lpEnumServiceStatus()         AS ENUM_SERVICE_STATUS
        LOCAL lngBytesNeeded                AS LONG
        LOCAL lpServicesReturned            AS DWORD
        LOCAL hNextUnreadEntry              AS DWORD
        LOCAL lngStructsNeeded              AS LONG
        LOCAL lngServiceStatusInfoBuffer    AS LONG
        LOCAL strServiceName                AS ASCIIZ * 250
        LOCAL i                             AS LONG
    
        'local lngServiceType as long (either %SERVICE_ACTIVE or
        '                                       %SERVICE_ACTIVE Or %SERVICE_INACTIVE
        'strText is the name to search, it is always the ServiceName
    
        hSCManager = OpenSCManager(BYVAL 0, BYVAL 0, %SC_MANAGER_ENUMERATE_SERVICE)
        IF hSCManager THEN
            hNextUnreadEntry = 0
            x = EnumServicesStatus(hSCManager, %SERVICE_WIN32, lngServiceType, dx, &H0, _
                     lngBytesNeeded, lpServicesReturned, hNextUnreadEntry)
    
           'We should receive %MORE_DATA error.
           IF NOT GetLastError() = %ERROR_MORE_DATA THEN
                EXIT FUNCTION
           ELSE
    
                'Calculate the number of structures needed
                lngStructsNeeded = lngBytesNeeded / LEN(lpEnumServiceStatus(0)) + 1
                'Redimension the array according to our calculation
                REDIM lpEnumServiceStatus(lngStructsNeeded - 1)
                'Get buffer size in bytes
                lngServiceStatusInfoBuffer = lngStructsNeeded * LEN(lpEnumServiceStatus(0))
                'Get services information starting entry 0
                hNextUnreadEntry = 0
                x = EnumServicesStatus(hSCManager, _
                                       %SERVICE_WIN32, _
                                       lngServiceType, _
                                       lpEnumServiceStatus(0), _
                                       lngServiceStatusInfoBuffer, _
                                       lngBytesNeeded, _
                                       lpServicesReturned, _
                                       hNextUnreadEntry)
                IF x = 0 THEN
                    'failed, call GetLastError why
                ELSE
                    'clear the listboxes
    
                    IF lstServiceName > 0 THEN SendMessage lstServiceName, %LB_RESETCONTENT, 0, 0
                    IF lstDisplayName > 0 THEN SendMessage lstDisplayName, %LB_RESETCONTENT, 0, 0
                    FOR i = 0 TO lpServicesReturned - 1
                        x = lstrcpy(strServiceName, BYVAL lpEnumServiceStatus(i).lpDisplayName)
                        IF lstDisplayName > 0 THEN SendMessage lstDisplayName, %LB_ADDSTRING, 0, VARPTR(strServiceName)
    
                        x = lstrcpy(strServiceName, BYVAL lpEnumServiceStatus(i).lpServiceName)
                        IF lstServiceName > 0 THEN SendMessage lstServiceName, %LB_ADDSTRING, 0, VARPTR(strServiceName)
                        IF strText > "" THEN
                            IF LCASE$(strText) = LCASE$(strServiceName) THEN FUNCTION = %TRUE
                        END IF
                    NEXT
                END IF
            END IF
            'Clean up.
            CloseServiceHandle hSCManager
    
        END IF
    END FUNCTION
    
    
    
    'MESSAGE http://www.powerbasic.com/support/forums/Forum4/HTML/012154.html
    FUNCTION servicestop(sservice AS STRING) AS LONG
    FUNCTION=0
      LOCAL hSCM AS DWORD, hSrvc AS DWORD, dwResult AS DWORD
      LOCAL SS AS SERVICE_STATUS, szStr AS ASCIIZ * 128
      hSCM = OpenSCManager(BYVAL 0, BYVAL 0, %SC_MANAGER_ALL_ACCESS)
      IF hSCM THEN
        szStr = sservice
        hSrvc = OpenService(hSCM, szStr, %SERVICE_STOP OR %GENERIC_WRITE)
        IF hSrvc THEN
          dwResult = ControlService(hSrvc, %SERVICE_CONTROL_STOP, SS)
          IF dwResult THEN FUNCTION=1&
        END IF
      END IF
      IF (hSrvc=0) OR (hSCM = 0) OR (dwResult=0) THEN FUNCTION=0&
      CloseServiceHandle hSrvc
      CloseServiceHandle hSCM
    END FUNCTION
    
    
    
    'MESSAGE http://www.powerbasic.com/support/forums/Forum7/HTML/002086.html
    FUNCTION StartManually(sservice AS STRING) AS LONG
       LOCAL hSCM     AS DWORD
       LOCAL hService AS DWORD
       LOCAL  gzServiceName    AS ASCIIZ * 64
       gzservicename=sservice
       FUNCTION=0
       hSCM = OpenSCManager(BYVAL %Null, BYVAL %Null, %SC_MANAGER_ALL_ACCESS)  'Open the SC Manager
       IF hSCM THEN                                                            'Got a handle to SCM.
          hService = OpenService(hSCM, gzServiceName, %SERVICE_ALL_ACCESS)     'Get the service handle
          IF hService THEN
             IF  StartService(hService, 0, 0) THEN FUNCTION = 1&            'start the service
          END IF
       END IF
       CloseServiceHandle(hService)
       CloseServiceHandle(hSCM)
    END FUNCTION
    
    
    
    FUNCTION PBMAIN() AS LONG
    
     DIM I AS LONG
     DIM J AS LONG
     DIM K AS LONG
     DIM L AS LONG
     DIM M AS LONG
     DIM N AS LONG
    
    
     DIM sfilenametemp         AS STRING
     DIM sfilenametemp2        AS STRING
     DIM iflagofproxyservice AS LONG
     DIM sproxyservice AS STRING
     DIM sproxypluslogdirectory       AS STRING
     DIM sproxypluslogfilenamebase(6) AS STRING
     DIM sproxypluslogfilenameext     AS STRING
     DIM sproxypluslogfilename        AS STRING
    
        sproxyservice="ProxyPlus"
        sproxypluslogdirectory=         TRIM$("C:\Program Files\ProxyPlus\Logs                                                               ")
        sproxypluslogfilenamebase(1)=   TRIM$("AccessLog                                                                                    ")
        sproxypluslogfilenamebase(2)=   TRIM$("ProxyLog                                                                                     ")
        sproxypluslogfilenamebase(3)=   TRIM$("DialLog                                                                                      ")
        sproxypluslogfilenamebase(4)=   TRIM$("MailLog                                                                                      ")
        sproxypluslogfilenamebase(5)=   TRIM$("ErrLog                                                                                       ")
        sproxypluslogfilenamebase(6)=   TRIM$("SecLog                                                                                       ")
        sproxypluslogfilenameext    =   TRIM$(".TXT                                                                                          ")
    
        iflagofproxyservice=0&
    
      n=0&
    trytostopproxyservice:
      INCR n
      IF ISTRUE IsServiceRunning (%SERVICE_ACTIVE, sproxyservice, 0, 0) THEN
          iflagofproxyservice=1&
          servicestop("Proxyplus")
          SLEEP 500
       END IF
     IF n<10& THEN IF ISTRUE IsServiceRunning (%SERVICE_ACTIVE, sproxyservice, 0, 0) THEN SLEEP 1000:GOTO trytostopproxyservice
     IF ISTRUE IsServiceRunning (%SERVICE_ACTIVE, sproxyservice, 0, 0) THEN FUNCTION=1:EXIT FUNCTION
    
    
    'check for existence of current log files before rotating files
    'if the current files do not exist the abort this program
        FOR K = 1& TO 6&
          sfilenametemp = sproxypluslogdirectory+"\"+sproxypluslogfilenamebase(K)+sproxypluslogfilenameext
          sfilenametemp=UCASE$(sfilenametemp)
          IF ISFILE(sfilenametemp) THEN GOTO findoldestlogfile
        NEXT K
    
       FUNCTION=0:
       EXIT FUNCTION ' there are not log files
    
     ' find the file name with the highest number in it, this will be the oldest log file
    findoldestlogfile:
        M = 0&
        L = 0&
        FOR I = 0& TO 99999&
        IF (I MOD 10&)= 0& THEN SLEEP 30&
        IF L = 300& THEN EXIT FOR  ' this stop the program if more than 50 straight log files are not found
                               ' proxyplus seems to limit log files to a maximum of 31 logfiles on a rollover basis
        FOR K = 1& TO 6&
        sfilenametemp=sproxypluslogdirectory+"\"+sproxypluslogfilenamebase(k)+"_"+TRIM$(STR$(I))+sproxypluslogfilenameext
        IF NOT ISFILE(sfilenametemp)THEN INCR L:ITERATE
        IF ISFILE(sfilenametemp)THEN M=I:L=0&:ITERATE:ITERATE
        NEXT K
        NEXT I
    IF M = 0& THEN GOTO mainlogfiles'
    
      FOR I = M TO 1&  STEP -1&
        FOR K = 1& TO 6&
        sfilenametemp  = sproxypluslogdirectory+"\"+sproxypluslogfilenamebase(K)+"_"+TRIM$(STR$(I))+sproxypluslogfilenameext
        sfilenametemp=UCASE$(sfilenametemp)
        sfilenametemp2 = sproxypluslogdirectory+"\"+sproxypluslogfilenamebase(K)+"_"+TRIM$(STR$(I+1&))+sproxypluslogfilenameext
        sfilenametemp2=UCASE$(sfilenametemp2)
        NAME sfilenametemp AS sfilenametemp2
        NEXT K
      NEXT I
    
    mainlogfiles:
        FOR K = 1& TO 6&
        sfilenametemp  = sproxypluslogdirectory+"\"+sproxypluslogfilenamebase(K)+sproxypluslogfilenameext
        sfilenametemp=UCASE$(sfilenametemp)
        sfilenametemp2 = sproxypluslogdirectory+"\"+sproxypluslogfilenamebase(K)+"_"+TRIM$(STR$(1&))+sproxypluslogfilenameext
        sfilenametemp2=UCASE$(sfilenametemp2)
        NAME sfilenametemp AS sfilenametemp2
        sfilenametemp2 = sproxypluslogdirectory+"\"+sproxypluslogfilenamebase(K)+"_"+TRIM$(STR$(0&))+sproxypluslogfilenameext
        sfilenametemp2=UCASE$(sfilenametemp2)
        KILL sfilenametemp2
        KILL sfilenametemp
        NEXT K
    
      IF iflagofproxyservice = 0& THEN FUNCTION=0:EXIT FUNCTION
    
    
      'restartbackup service if the proxyplus service was running when this program started
      n=0&
      i=0&
    trytostartproxyservice:
    'a little duplication of check never hurt as in below
    FUNCTION=1&
        INCR n
        i=StartManually(sproxyservice)
        IF i THEN FUNCTION=0:EXIT FUNCTION
        IF n<10& THEN
             IF ISFALSE IsServiceRunning (%SERVICE_ACTIVE, sproxyservice, 0, 0) THEN
             SLEEP 400
             GOTO trytostartproxyservice
             END IF
         END IF
     IF ISTRUE IsServiceRunning (%SERVICE_ACTIVE, sproxyservice, 0, 0) THEN FUNCTION=0
    
    END FUNCTION
    Attached Files

    Leave a comment:


  • Paul Purvis
    replied
    refinements made to the code and a replaced zipped file.

    the zipped file also contains the program "file2ftp.exe" which will follow all instructions in the ini file to transfer whatever files you wish to a ftp server, it is not hard coded for proxyplus log files that are stored using the pplogkpr.exe program listed above.

    if your ftp server does not let you create the directory and or change to a particular directory on the ftp server, then this program is designed to fail in order to not step on or place files in unwanted ftp server directories.

    Other than the linux sme server, the ftp server software written by TYPSOFT was tested "TYPSOFT FTP SERVER VER 1.11" a freeware windows os ftp server, i have used in the past that has been stable enough for most needs.

    Leave a comment:


  • Paul Purvis
    replied
    program to transfer proxyplus logged files via ftp to ftpserver using port #21 only
    easily to edit program to transfer any files, see notes below.

    I used Michael's method of getting source code.
    Sincerely thanks to Michael for the great remarks made in his work, and to John as well, with out their hard work up from that made this job a whole lot easier and even possible.

    This program is hard coded to retrieve the log files archived in the previous listing and send them to a ftp server.
    The program will create directories on the ftp server regardless of whether they exist or not.
    In testing this program, the ftp server being a linux server, there was going to be a problem. Linux sees upper case and lower case as two different file names in most cases.

    I choose to make all the directories and file names in upper case just so if you happen to network to the server and run some dos programs that keep file names in uppercase there would be no problems. Although i find lower case easier to read. If your ftp server is a windows os, there should be no problem.

    one function i placed in this code was to build a directory on the ftp server.
    this program was designed to just run in the back ground, not as a service, but just as a hidden program.

    The location of the source files to send are hard coded in this program.
    To make a change to allow files from the ini file, just remove the two lines of code the you see this "c:\systemplus\proxypluslogs\". And that should work.

    If you place the word "LOOPTIME=XXX" like LOOPTIME=5, this program when ran will loop every 5 minutes xxx=minutes.

    you can place the name of the ini file on the command line
    if not, it will search in the current directory for a file with the same name as the exe but have a ini extension on it.
    If it does not find it there, it will look for the ini file file where the exe file was actually located.

    If you do not place in the ini file a location of where to place the files on the ftp server, they should go into the root directory of where you logged in.

    I am tired and going to bed now.
    I have tested this program on a linux ftp server.
    any files should be overwritten on the ftp server when this program is ran.

    This program will try something like 4 or 5 times to make a ftp connection before it stops and it does not log anything or display anything.

    I trimmed out the remarks to be able to see my code on a laptop screen.
    This program is really a starting place for another program that will read a ini file and transfer files.



    program
    Code:
    ' WININET_FTP_UPLOAD.bas
    ' Author: Michael Mattias Tal Systems Inc Racine WI
    ' Use: Placed in public domain by author 3/9/08
    ' Credits: John McWilliams earlier in this source code posting
    ' ----------------------------------------------
    'compiled with pbwin9.01
    
    #COMPILE EXE
    #DIM     ALL
    
    ' ----------------------------------------
    ' STANDARD WINDOWS HEADER FILES AND DATES
    ' ----------------------------------------
    
    #INCLUDE "WIN32API.INC"
    #IF NOT %DEF(%INVALID_HANDLE_VALUE_LONG)
      %INVALID_HANDLE_VALUE_LONG = -1&
    #ENDIF
    #INCLUDE "Wininet.inc"
    #IF NOT %DEF (%SYSEMT_INC)
     %SYSEMT_INC  = 1
    
    FUNCTION SystemErrorMessageText (BYVAL ECode AS LONG) AS STRING
      LOCAL Buffer AS ASCIIZ * 255
      FormatMessage %FORMAT_MESSAGE_FROM_SYSTEM, BYVAL %NULL, ECode, %NULL, buffer, SIZEOF(buffer), BYVAL %NULL
      FUNCTION = FORMAT$(ECode, "##### ") & Buffer
    END FUNCTION
    
    #ENDIF
    DECLARE FUNCTION GetFileSizebyName (szFile AS ASCIIZ) AS LONG
    DECLARE FUNCTION buildftpdirectory( hIConnect AS LONG,stemp AS STRING) AS LONG
    
    ' -----------------------
    ' MACROS of CONVENIENCE
    ' -----------------------
    MACRO bva(anything)      = BYVAL VARPTR(anything)
    MACRO bvaz(asciizString) = BYVAL IIF(lstrlen(asciizString), VARPTR(AsCIIZString), %NULL)
    
    
    FUNCTION GetFileSizebyName (szFile AS ASCIIZ) AS LONG
     LOCAL w32 AS WIN32_FIND_DATA, hSearch AS LONG
     hSearch = FindFirstFile (szFile, W32)
     IF       hSearch <> %INVALID_HANDLE_VALUE_LONG THEN
         FindClose    hSearch
         FUNCTION   = W32.nFileSIzeLow
     ELSE
         FUNCTION = -1&  ' artificial "NOTFOUND" value
     END IF
    END FUNCTION
    
    
    FUNCTION buildftpdirectory( hIConnect AS LONG,stemp AS STRING) AS LONG
    'stemp is the directory name you wish to build on the ftp server
        LOCAL i AS LONG
        LOCAL j AS LONG
        LOCAL k AS LONG
        LOCAL b() AS STRING
        FUNCTION=0&
        LOCAL  szDirtemp AS ASCIIZ * %MAX_PATH
        stemp="/"+TRIM$(stemp)+"/"
        REPLACE "//" WITH "/" IN stemp
        stemp=" "+stemp+" "
        cleandirectoryname:
        REPLACE " /" WITH "/" IN stemp
        REPLACE "/ " WITH "/" IN stemp
        IF INSTR(stemp," /") GOTO cleandirectoryname
        IF INSTR(stemp,"/ ") GOTO cleandirectoryname
        stemp=" "+TRIM$(stemp)+" "
        IF INSTR(stemp," /") THEN REPLACE " /" WITH "" IN stemp:GOTO cleandirectoryname
        IF LEN(stemp)=0 THEN FUNCTION=1:EXIT FUNCTION
        stemp=TRIM$(stemp)
        IF INSTR(stemp,"/")=0 THEN J=1  ELSE J= PARSECOUNT(stemp,"/")
        REDIM b(1 TO j)
        IF J=1 THEN b(1)=stemp ELSE PARSE stemp, b(),"/"
        szdirtemp="/"
        k = FtpSetCurrentDirectory ( hiconnect,szdirtemp)
        IF NOT ISTRUE k THEN FUNCTION=0:EXIT FUNCTION
        SLEEP 30
        FOR I=1 TO J
        IF LEN(TRIM$(b(i)))=0 THEN EXIT FOR
        Szdirtemp=b(i)
        k = FtpCreateDirectory ( hiconnect,szdirtemp)
        k = FtpSetCurrentDirectory ( hiconnect,szdirtemp)
        IF NOT ISTRUE k THEN FUNCTION=0:EXIT FUNCTION
        NEXT i
        FUNCTION=1&
    END FUNCTION
    
    
    
    FUNCTION PBMAIN() AS LONG
      DIM i AS LONG
      DIM j AS LONG
      DIM scommandline AS STRING
      DIM sloop AS STRING
      DIM iloop AS LONG
    
    
      scommandline=" "+TRIM$(UCASE$(COMMAND$))+" "
      j=INSTR(scommandline,"LOOPTIME=")
      IF j THEN
         sloop=MID$(scommandline,j,(INSTR(j+9,scommandline," ")-j))
         REPLACE sloop WITH "" IN scommandline
         scommandline=TRIM$(scommandline)
      END IF
      IF j THEN
      sloop=sloop+" "
      iloop=VAL(RIGHT$(sloop,(LEN(sloop)-9)))
      END IF
    
      j=0&
      hangaroundinmemoryasaloop:
      j=j+1&
      FUNCTION=0
      i= Uploadfiles (scommandline)
      'this will retry on 10 times within 4 seconds each
      IF i<1& THEN
          IF j<5& THEN SLEEP 4000:GOTO hangaroundinmemoryasaloop
      END IF
      j=0&
      IF iloop THEN
          SLEEP (iloop*60000)
          GOTO hangaroundinmemoryasaloop
      END IF
    
      FUNCTION=i
    
    END FUNCTION
    
    
    
    FUNCTION Uploadfiles (scommandline AS STRING) AS LONG
        LOCAL szAgent       AS ASCIIZ * 256
        LOCAL szFtpServer   AS ASCIIZ * 256
        LOCAL rsvp          AS LONG
        LOCAL szRemoteFile    AS ASCIIZ * 256
        LOCAL szLocalFile     AS ASCIIZ * 256
        LOCAL hIOpen AS LONG
        LOCAL hiConnect AS LONG
        LOCAL dwContext    AS LONG
        LOCAL szUserId      AS ASCIIZ * %MAX_PATH
        LOCAL szPassword    AS ASCIIZ * %MAX_PATH
        LOCAL E             AS LONG
        LOCAL LE            AS LONG   ' last API Error
        LOCAL szLastResponseInfo AS ASCIIZ * 16384
        LOCAL cchLastResponseInfo AS LONG
        LOCAL iErrLastResponse     AS LONG
        LOCAL dwFlags AS DWORD
        LOCAL nBytesAppended AS LONG, nTotalBytesAppended AS LONG
        LOCAL nBytesDownloaded AS LONG, nTotalBytesDownloaded AS LONG
        LOCAL sMsg  AS STRING
        LOCAL szErrMsg   AS ASCIIZ * %MAX_PATH
        LOCAL szLastErrMsg  AS ASCIIZ * %MAX_PATH
        LOCAL fSize1 AS LONG, fSize2 AS LONG
        LOCAL iRet AS LONG, szDir AS ASCIIZ * %MAX_PATH, cbBuffer AS LONG
        LOCAL iRetDelete AS LONG
        LOCAL szftpserverdirectory AS ASCIIZ * %MAX_PATH, sztempdir AS ASCIIZ * %MAX_PATH
    
    
        LOCAL stodaysdate AS STRING
        LOCAL suserfile AS STRING
        LOCAL suserdirectory AS STRING
        LOCAL lfileno AS LONG
        LOCAL stemp AS STRING
        LOCAL sftp_server AS STRING
        LOCAL suser_id AS STRING
        LOCAL spassword AS STRING
        LOCAL sfile_target AS STRING
        LOCAL sfile_source AS STRING
        LOCAL slocallocationname AS STRING
        LOCAL slocalcomputername AS STRING
        LOCAL sftpserverdirectory AS STRING
        LOCAL sinifilename AS STRING
        LOCAL sfilestosendname AS STRING
        LOCAL sfilestosenddirectory AS STRING
    
        FUNCTION=0
        sinifilename=UCASE$(TRIM$(scommandline))
        IF LEN(sinifilename) THEN IF ISFILE(sinifilename) THEN GOTO readinifile
        sinifilename=UCASE$(EXE.NAME$)
        sinifilename=LEFT$(sinifilename,LEN(sinifilename)-4)+".INI"
        IF LEN(sinifilename) THEN IF ISFILE(sinifilename) THEN GOTO readinifile
        sinifilename=UCASE$(EXE.FULL$)
        sinifilename=LEFT$(sinifilename,LEN(stemp)-4)+".INI"
        IF LEN(sinifilename) THEN IF ISFILE(sinifilename) THEN GOTO readinifile
        FUNCTION=0
        EXIT FUNCTION
    
    
    readinifile:
         sinifilename=TRIM$(sinifilename)
         lfileno = FREEFILE
         TRY
         OPEN sinifilename FOR INPUT  ACCESS READ LOCK WRITE AS #lfileno
         CATCH
         CLOSE #lfileno
         FUNCTION=0
         EXIT FUNCTION
         EXIT TRY
         FINALLY
         IF LOF(lfileno) = 0& THEN CLOSE #lfileno:EXIT FUNCTION
         LINE INPUT #lfileno, sftp_server
         LINE INPUT #lfileno, suser_id
         LINE INPUT #lfileno, spassword
         LINE INPUT #lfileno, slocallocationname
         LINE INPUT #lfileno, slocalcomputername
         LINE INPUT #lfileno, sftpserverdirectory
         LINE INPUT #lfileno, sfilestosenddirectory
         LINE INPUT #lfileno, sfilestosendname
         CLOSE #lfileno
         END TRY
    
    
         sfilestosenddirectory=TRIM$(sfilestosenddirectory)
         sfilestosendname=TRIM$(sfilestosendname)
    
         IF LEN(sfilestosenddirectory)=0 THEN FUNCTION=0:EXIT FUNCTION
         IF LEN(sfilestosendname)=0 THEN FUNCTION=0:EXIT FUNCTION
    
         stodaysdate=TRIM$(DATE$)
         stodaysdate=MID$(stodaysdate,7,4)+MID$(stodaysdate,1,2)
         sftpserverdirectory=sftpserverdirectory+"/"+stodaysdate
         REPLACE "\" WITH "/" IN sftpserverdirectory
         sftpserverdirectory="/"+TRIM$(sftpserverdirectory)
         cleandirectoryname:
         REPLACE "/ " WITH "/" IN sftpserverdirectory
         REPLACE " /" WITH "/" IN sftpserverdirectory
         REPLACE "//" WITH "/" IN sftpserverdirectory
         IF INSTR(sftpserverdirectory,"/ ") THEN GOTO cleandirectoryname
         IF INSTR(sftpserverdirectory," /") THEN GOTO cleandirectoryname
    
    
         slocallocationname=TRIM$(slocallocationname)
         slocalcomputername=TRIM$(slocalcomputername)
    
         szftpServer    = TRIM$(sftp_server)
         szUserid      = TRIM$(suser_id)
         szPassword  = TRIM$(spassword)
         szAgent = "WinInet FTP Upload Demo"
         sftpserverdirectory=UCASE$(sftpserverdirectory)
         szftpserverdirectory=sftpserverdirectory
         szLocalFile =  sfile_source
         szRemoteFile = sfile_target
        ' Open an Internet session as predefined in the registry
        hIOpen = InternetOpen(BYVAL VARPTR(szAgent), _
                              BYVAL %INTERNET_OPEN_TYPE_PRECONFIG, _
                              BYVAL %NULL, _
                              BYVAL %NULL, _
                              BYVAL 0)
        IF hIOpen = %NULL THEN GOTO ERRORINFTP
    
    
        ' -------------------------------------
        '      CONNECT TO THE FTP SERVER
        ' -------------------------------------
    
        cchLastResponseInfo =   SIZEOF(szLastResponseInfo)
        dwFlags             =   %INTERNET_FLAG_PASSIVE
        hIConnect = InternetConnect(BYVAL hIOpen, _
                                    BYVAL VARPTR(szFtpServer), _
                                    BYVAL %INTERNET_DEFAULT_FTP_PORT, _
                                    bvaz (szUserId), _
                                    bvaz (szPassword), _
                                    BYVAL %INTERNET_SERVICE_FTP, _
                                    BYVAL dwFlags, _
                                    BYVAL 0)
    
    
        LE   = GetLastError
        CALL  InternetGetLastResponseInfo (iErrLastResponse, szLastResponseInfo, cchLastResponseInfo)
    
        IF hIConnect = %NULL THEN GOTO ERRORINFTP
        SLEEP 30
        cbBuffer  =   SIZEOF(szDir) - 1
        iRet  = FtpGetCurrentDirectory (hIConnect, szDir, cbBuffer)
        IF NOT ISTRUE Iret THEN GOTO ERRORINFTP
        SLEEP 30
        iret = buildftpdirectory( hIConnect,sftpserverdirectory)
        IF NOT ISTRUE iret THEN GOTO ERRORINFTP
        SLEEP 30
       ' iRet = FtpCreateDirectory ( hIConnect,szftpserverDirectory)
       ' IF NOT ISTRUE Iret THEN EXIT FUNCTION
        iRet = FtpSetCurrentDirectory ( hIConnect,szftpserverdirectory)
        IF NOT ISTRUE Iret THEN GOTO ERRORINFTP
        SLEEP 30
        cbBuffer  =   SIZEOF(szDir) - 1
        iRet  = FtpGetCurrentDirectory (hIConnect, szDir, cbBuffer)
        IF NOT ISTRUE Iret THEN GOTO ERRORINFTP
        SLEEP 30
    
    stemp =sfilestosenddirectory+"\"+sfilestosendname
    stemp ="c:\systemplus\proxypluslogs\"+stodaysdate+"\*.*"
    REPLACE " \" WITH "\" IN stemp
    REPLACE "\\" WITH "\" IN stemp
    
    stemp = DIR$(stemp,39)' %normal or %hidden or %system )
    getfilesanduploadthem:
    WHILE LEN(stemp)
    szlocalfile=sfilestosenddirectory+"\"+stemp
    szlocalfile="c:\systemplus\proxypluslogs\"+stodaysdate+"\"+stemp
       szremotefile=UCASE$(stemp)
         fsize1       = GetFileSizeByName (szLocalFile)
       dwFlags = %FTP_TRANSFER_TYPE_BINARY
       dwContext = %NULL    ' not using callbacks, so it's moot
       iRet  = FTPPutFile (hIConnect, szLocalFile, szRemoteFile, dwFlags, %NULL)
       LE    = GetLastError
       szLastErrMsg = SystemErrorMessageText (LE)
       CALL     InternetGetLastResponseInfo (iErrLastResponse, szLastResponseInfo, SIZEOF(szLastResponseInfo) -1)
     stemp=DIR$(NEXT)
     SLEEP 30
    WEND
    
    FUNCTION=1
    GOTO SUCCESSINFTP
    
    ERRORINFTP:
    FUNCTION=0
    
    SUCCESSINFTP:
        InternetCloseHandle (hIConnect)
        InternetCloseHandle (hIOpen)
    END FUNCTION
    [/code]
    ini file
    notice the "/" on the 6th line for the ftp server
    like i said also, all directories and files will be transferred and saved in upper case
    Code:
    www.myftpwebsite.com
    username
    userpassword
    this line not used
    this line not used
    targetftpsubdirectory/targetftpsubdirectory/targetftpsubdirectory
    c:\sourcedirectory\sourcesubdirectory
    *.txt
    Attached Files
    Last edited by Paul Purvis; 17 Sep 2009, 01:42 AM.

    Leave a comment:


  • Paul Purvis
    replied
    I am hoping this is my final copy of the proxyplus log keeper.
    this program runs as a service "program install" and "program uninstall"
    I do not like long names for my programs so i renamed this program and did some corrections and adjustments.
    Because the log files keep by proxyplus are readable in shared mode as i found out. This program now reads the current open log file and creates a log file in the archieve section of "*_0.TXT".

    The program waits 4 minutes after bootup before any archiving takes place and now loops to get files every 2 minutes rather that 1 one minute because the current log file might get to be large.

    Proxyplus does not rotate the files upon bootup but only by two methods. Automatic at midnight only or manually through a web menu interface.
    This becomes a problem for me because i wanted the proxyplus program to run on every computer and it basically serves itself. I wanted a log of internet activity. But laptops and other machines maybe off during the night, it is not going to automatically roll over the log files. The only course maybe to stop the proxyplus service, roll the files over using another program, then start the proxy service program back up.

    A change in the program is also now to save the file's names in upper case, because i have a program now that transfer the files by ftp to a ftp server.
    more on that after listing the program.

    this is probably going to be the last edition of this program.

    Code:
    'proxypluslogkeeper.bas
    'pbwin 9.01 complier
    'this program only runs as a service
    'place the exe in the windows directory
    'to install the program enter "proxypluslogkeeper install" then reboot
    'to remove  the program enter "proxypluslogkeeper uninstall" then reboot
    'if you use stop the service with console command "net stop proxyplus log keeper" you cannot
    '         restart the service using the "net start" command, you have to reboot to restart the service
    '
    
    '
    ' this program reads log files from proxy+ (proxyplus) version 3.09 build 264
    ' then saves them else where on the hard drive
    ' the directory  created is "\systemplus" that is hidden in the root directory
    ' a subdirectory in the directory  \systemplus is created called proxyplus where
    ' log files are placed by date
    'ex "c:\systemplus\proxyplus\20091231"
    'files names are incremented to keep from losing log data from the proxyplus
    'after the log files from proxyplus are read and new ones created under the
    '"\systemplus\proxyplus" directory, the original log files are deleted.
    
    'you can set the number of logs to create and the number of days in each period before logs are archieved(rolledover)
    
    'there seems to be a maximum of 31 periods
    '
    'proxyplus keeps data sinformation in the registry
    'there is information in the registry but at this time this program does not read or make changes to the registry
    
    
    
    
    #COMPILE EXE  "pplogkpr.exe"
    #DIM ALL
    #REGISTER NONE
    #INCLUDE "WIN32API.INC"
    
    GLOBAL hWindow AS LONG ' You should set this in your WINMAINX to the handle
    ' of your main window.
    
    FUNCTION winmainx() AS LONG
    
     DIM pathlogdirectory     AS STRING
     DIM newpathlogdirectory  AS STRING
     DIM todaysdate           AS STRING
     DIM fileexist            AS LONG
     DIM filenametemp         AS STRING
     DIM homedrive            AS STRING
     DIM ProgramFiles         AS STRING
     DIM temp                 AS STRING
    
     DIM I AS LONG
     DIM J AS LONG
     DIM K AS LONG
     DIM L AS LONG
     DIM M AS LONG
    
     DIM proxypluslogdirectory       AS STRING
     DIM proxypluslogfilenamebase(6) AS STRING
     DIM proxypluslogfilenameext     AS STRING
     DIM proxypluslogfilename        AS STRING
     DIM newlogfilename              AS STRING
     DIM fileno1                     AS LONG
     DIM fileno2                     AS LONG
     DIM filecontents                AS STRING
    
    
    SLEEP 240000  ' wait 4 minutes before the program starts trying to reading the log files after a bootup or restart
    
    
    
    
    
    ' some of these variables are in the registry, but this program does not read the registry
    
        proxypluslogdirectory=         TRIM$("C:\Program Files\ProxyPlus\Logs                                                               ")
        proxypluslogfilenamebase(1)=   TRIM$("AccessLog                                                                                    ")
        proxypluslogfilenamebase(2)=   TRIM$("ProxyLog                                                                                     ")
        proxypluslogfilenamebase(3)=   TRIM$("DialLog                                                                                      ")
        proxypluslogfilenamebase(4)=   TRIM$("MailLog                                                                                      ")
        proxypluslogfilenamebase(5)=   TRIM$("ErrLog                                                                                       ")
        proxypluslogfilenamebase(6)=   TRIM$("SecLog                                                                                       ")
        proxypluslogfilenameext    =   TRIM$(".TXT                                                                                          ")
    
    
    
        startprocessinglogfiles:
    
        'wait 4 minutes if the time is between 2 minutes till midnight and 5 minutes after midnight
        'this gives the proxyplus service time to create new log files that are suppose to be create automatically
        '    around midnight on any given day according to the period setup in proxyplus
        I = TIMER
        IF I > 86280 OR I < 300 THEN SLEEP 240000:GOTO startprocessinglogfiles
    
    
        homedrive=ENVIRON$("HOMEDRIVE")
        ProgramFiles=ENVIRON$("ProgramFiles")
    
        pathlogdirectory=homedrive+"\systemplus"
        MKDIR pathlogdirectory
    
        SETATTR pathlogdirectory, %HIDDEN + %SYSTEM + %NORMAL
    
        pathlogdirectory=pathlogdirectory+"\proxypluslogs"
        MKDIR pathlogdirectory
    
        logitagain:
        todaysdate=DATE$
    '   next line will store all logs in a directory by a specfic year and month
        newpathlogdirectory=pathlogdirectory+"\"+MID$(todaysdate,7,4)+MID$(todaysdate,1,2)
    '   unremarking the next line will store all logs in a directory by a specfic year, month and day
    '    newpathlogdirectory=pathlogdirectory+"\"+MID$(todaysdate,7,4)+MID$(todaysdate,1,2)+MID$(todaysdate,4,2)
    
        MKDIR newpathlogdirectory
    
      FOR K = 1& TO 6&
        filecontents = ""
        filenametemp = proxypluslogdirectory+"\"+proxypluslogfilenamebase(K)+proxypluslogfilenameext
        filenametemp=UCASE$(filenametemp)
        IF NOT ISFILE(filenametemp)THEN ITERATE
         TRY
        fileno1 = FREEFILE
         OPEN filenametemp FOR BINARY ACCESS READ  LOCK SHARED AS #fileno1
         CATCH
         CLOSE #fileno1
         EXIT TRY
         FINALLY
           IF LOF(fileno1) = 0& THEN
               CLOSE #fileno1
           EXIT TRY
          END IF
         GET$ fileno1, LOF(fileno1),filecontents
         CLOSE #fileno1
         L = 0&
        END TRY
    
        filenametemp = newpathlogdirectory+"\"+proxypluslogfilenamebase(K)+"_0"+proxypluslogfilenameext
        filenametemp=UCASE$(filenametemp)
        KILL filenametemp
        TRY
         fileno1 = FREEFILE
         OPEN filenametemp FOR BINARY ACCESS READ WRITE  LOCK WRITE AS #fileno1
         CATCH
         CLOSE #fileno1
         EXIT TRY
         FINALLY
         PUT$ fileno1, filecontents
         CLOSE #fileno1
        END TRY
      NEXT k
    
    
    
    
     ' find the file name with the highest number in it, this will be the oldest log file
        M = 0&
        L = 0&
        FOR I = 0& TO 99999&
        IF (I MOD 10&)= 0& THEN SLEEP 30&
        IF L = 300& THEN EXIT FOR  ' this stop the program if more than 50 straight log files are not found
                               ' proxyplus seems to limit log files to a maximum of 31 logfiles on a rollover basis
        FOR K = 1& TO 6&
        filenametemp=proxypluslogdirectory+"\"+proxypluslogfilenamebase(k)+"_"+TRIM$(STR$(I))+proxypluslogfilenameext
        filenametemp=UCASE$(filenametemp)
        IF NOT ISFILE(filenametemp)THEN INCR L:ITERATE
        IF ISFILE(filenametemp)THEN M=I:L=0&:ITERATE:ITERATE
        NEXT K
        NEXT I
       IF M = 0& THEN GOTO endprocessing
    
    
       FOR I = M TO 1&  STEP -1&
        FOR K = 1& TO 6&
        filecontents = ""
        filenametemp = proxypluslogdirectory+"\"+proxypluslogfilenamebase(K)+"_"+TRIM$(STR$(I))+proxypluslogfilenameext
        filenametemp=UCASE$(filenametemp)
        IF NOT ISFILE(filenametemp)THEN ITERATE
         TRY
        fileno1 = FREEFILE
         OPEN filenametemp FOR BINARY ACCESS READ LOCK WRITE AS #fileno1
         CATCH
         CLOSE #fileno1
         EXIT TRY
         FINALLY
           IF LOF(fileno1) = 0& THEN
               CLOSE #fileno1
               KILL filenametemp
               ITERATE
           EXIT TRY
           END IF
                filecontents = ""
         GET$ fileno1, LOF(fileno1),filecontents
         CLOSE #fileno1
         L = 0&
        END TRY
    
    
    
       FOR J=1& TO 9999999&
          newlogfilename = newpathlogdirectory+"\"+proxypluslogfilenamebase(K)+"_"+TRIM$(STR$(J))+proxypluslogfilenameext
          newlogfilename=UCASE$(newlogfilename)
          IF ISFILE(newlogfilename)THEN ITERATE
    
          fileno2 = FREEFILE
          TRY
            OPEN newlogfilename FOR BINARY ACCESS READ WRITE LOCK READ WRITE AS #fileno2
          CATCH
            CLOSE #fileno2
            EXIT TRY
          FINALLY
            PUT$ fileno2, filecontents
            CLOSE #fileno2
            KILL filenametemp
            EXIT FOR
          END TRY
        NEXT J
    
        NEXT K
    
        NEXT I
    
    endprocessing:
        SLEEP 120000 'wait 120 seconds before scanning for new log files
        GOTO startprocessinglogfiles
    
    END FUNCTION
    
    
    
    ' ---------------------
    ' CONSTANT DECLARATIONS
    ' ---------------------
    %EVENTLOG_AUDIT_FAILURE = 16
    %EVENTLOG_AUDIT_SUCCESS = 8
    %EVENTLOG_ERROR_TYPE = 1
    %EVENTLOG_INFORMATION_TYPE = 4
    %EVENTLOG_SUCCESS = 0
    %EVENTLOG_WARNING_TYPE = 2
    
    ' This constant is used to set logging status.
    %EVENTLOG_LEVEL = %EVENTLOG_AUDIT_FAILURE + %EVENTLOG_AUDIT_SUCCESS + %EVENTLOG_ERROR_TYPE + _
    %EVENTLOG_INFORMATION_TYPE + %EVENTLOG_SUCCESS + %EVENTLOG_WARNING_TYPE
    %SERVICE_WIN32_OWN_PROCESS = &H10&
    %SERVICE_WIN32_SHARE_PROCESS = &H20&
    %SERVICE_WIN32 = %SERVICE_WIN32_OWN_PROCESS + %SERVICE_WIN32_SHARE_PROCESS
    %SERVICE_DEMAND_START = &H3&
    %SERVICE_ERROR_NORMAL = &H1&
    %WAIT_OBJECT_0 = 0&
    
    ' -------------------------
    ' FUNCTION/SUB DECLARATIONS
    ' -------------------------
    DECLARE FUNCTION Install() AS LONG
    DECLARE FUNCTION InitService() AS LONG
    DECLARE FUNCTION Uninstall() AS LONG
    DECLARE FUNCTION WinErrorMessage(BYVAL ErrNumber AS LONG) AS STRING
    DECLARE FUNCTION SendStatus(BYVAL CurrentStatus AS DWORD, BYVAL ExitCode AS DWORD, ServiceSpecificExitCode AS LONG, BYVAL Checkpoint AS DWORD, BYVAL WaitHint AS DWORD) AS LONG
    DECLARE FUNCTION ServiceThread(ID AS LONG) AS LONG
    DECLARE SUB Handler(BYVAL ControlValue AS DWORD)
    DECLARE SUB LogNTEvent(Message AS STRING, LogCode AS LONG)
    DECLARE SUB ServiceMain(BYVAL dwArgs AS DWORD, BYVAL lpszArgv AS DWORD)
    DECLARE SUB Terminate(ErrCode AS DWORD)
    DECLARE SUB StopService()
    DECLARE SUB PauseService()
    DECLARE SUB ResumeService()
    
    ' ----------------
    ' GLOBAL VARIABLES
    ' ----------------
    GLOBAL gascServiceName AS ASCIIZ * 8
    GLOBAL gascServiceDisplayName AS ASCIIZ * 256
    GLOBAL gdwdhStatus AS DWORD
    GLOBAL gdwdServiceState AS DWORD
    GLOBAL gdwdServiceStatus AS DWORD
    GLOBAL glnghInstance AS LONG
    GLOBAL glnghPrvInst AS LONG
    GLOBAL glnghTerminateEvent AS LONG
    GLOBAL glnghServiceThread AS LONG
    GLOBAL glngThreadResult AS LONG
    GLOBAL glngRunningService AS LONG
    GLOBAL glngPauseService AS LONG
    GLOBAL glngCmdShow AS LONG
    GLOBAL ptrCmdLine AS ASCIIZ PTR
    
    ' FUNCTION: WinMain
    FUNCTION WINMAIN(BYVAL hCurInst AS LONG, BYVAL hPrvInst AS LONG, BYVAL CmdLine AS ASCIIZ PTR, BYVAL CmdShow AS LONG) EXPORT AS LONG
    'FUNCTION PBMAIN () AS LONG
    
    DIM Result AS LONG
    DIM CmdParm AS STRING
    DIM udtSTE AS SERVICE_TABLE_ENTRY
    
    ' Set the service name and display name here.
    gascServiceName        = TRIM$("ProxyPlusLogKeeperService")
    gascServiceDisplayName = TRIM$("ProxyPlus Log Keeper")
    glngHInstance = hCurInst
    glnghPrvInst = hPrvInst
    glngCmdShow = CmdShow
    ptrCmdLine = CmdLine
    
    ' Get the Command Line parms
    CmdParm = UCASE$(EXTRACT$(@CmdLine, ANY " ,." + CHR$(9)))
    ' Get rid of any seperators
    CmdParm = REMOVE$(CmdParm, ANY "-\/")
    
    ' If the exe was executed with the Install parm then we just install and quit
    IF CmdParm = "INSTALL" THEN
    ' Install the service.
    Result = Install()
    IF ISFALSE Result THEN
    MSGBOX "An error occured while trying to install this service
    END IF
    ' If the exe was executed with the UnInstall parm then we just uninstall and quit
    ELSEIF CmdParm = "UNINSTALL" THEN
    ' Uninstall the service.
    Result = Uninstall()
    IF ISFALSE Result THEN
    MSGBOX "An error occured while trying to uninstall this service
    END IF
    
    ' No parms were given that we want so start up as service
    ' This should only happen when the service control manager starts us
    ELSE
    
    udtSTE.lpServiceName = VARPTR(gascServiceName)
    udtSTE.lpServiceProc = CODEPTR(ServiceMain)
    
    Result = StartServiceCtrlDispatcher(udtSTE)
    
    IF Result = 0 THEN
    ExitProcess GetLastError()
    END IF
    
    END IF
    
    FUNCTION = 0
    
    END FUNCTION
    
    
    
    
    
    
    ' Install the service into windows
    FUNCTION Install() AS LONG
    DIM dwdRet AS DWORD
    DIM lngDQEnd AS LONG
    DIM lngDQStart AS LONG
    DIM lngHSCManager AS LONG
    DIM lngHService AS LONG
    DIM ascEXE AS ASCIIZ * %MAX_PATH
    
    ON ERROR GOTO Error_Install
    
    ' Assume a failure for now.
    FUNCTION = %FALSE
    
    lngHSCManager = OpenSCManager(BYVAL %NULL, BYVAL %NULL, %SC_MANAGER_CREATE_SERVICE)
    
    IF lngHSCManager <> %NULL THEN
    ' OK, we have a handle to the SCM.
    ' Get the full EXE file path.
    dwdRet = GetModuleFileName(glngHInstance, ascEXE, %MAX_PATH)
    IF dwdRet <> 0 THEN
    ' Install the service.
    lngHService = CreateService(lngHSCManager, gascServiceName, gascServiceDisplayName, _
    %SERVICE_ALL_ACCESS, %SERVICE_WIN32_OWN_PROCESS, _
    %SERVICE_AUTO_START, %SERVICE_ERROR_NORMAL, _
    ascEXE, BYVAL %NULL, BYVAL %NULL, _
    BYVAL %NULL, BYVAL %NULL, BYVAL %NULL)
    ' Close any service handles.
    IF lngHService <> %NULL THEN
    ' Success!
    FUNCTION = %TRUE
    CloseServiceHandle lngHService
    END IF
    CloseServiceHandle lngHSCManager
    END IF
    END IF
    
    FUNCTION = %TRUE
    
    EXIT FUNCTION
    
    Error_Install:
    
    FUNCTION = -1& * ERR
    
    ON ERROR RESUME NEXT
    
    ' Close any service handles.
    IF lngHService <> %NULL THEN
    CALL CloseServiceHandle(lngHService)
    END IF
    
    IF lngHSCManager <> %NULL THEN
    CALL CloseServiceHandle(lngHSCManager)
    END IF
    
    END FUNCTION
    
    ' Unistall the service
    FUNCTION Uninstall() AS LONG
    DIM lngHSCManager AS LONG
    DIM lngHService AS LONG
    
    ON ERROR GOTO Error_Uninstall
    
    ' Assume a failure for now.
    FUNCTION = %FALSE
    
    lngHSCManager = OpenSCManager(BYVAL %NULL, BYVAL %NULL, %SC_MANAGER_CREATE_SERVICE)
    
    IF lngHSCManager <> %NULL THEN
    ' OK, we have a handle to the SCM.
    ' Now open our service.
    lngHService = OpenService(lngHSCManager, gascServiceName, %SERVICE_ALL_ACCESS)
    IF lngHService <> %NULL THEN
    ' Delete the service.
    IF DeleteService(lngHService) <> %NULL THEN
    ' Success!
    FUNCTION = %TRUE
    END IF
    CloseServiceHandle lngHService
    END IF
    CloseServiceHandle lngHSCManager
    END IF
    
    EXIT FUNCTION
    
    Error_Uninstall:
    
    FUNCTION = -1& * ERR
    
    ON ERROR RESUME NEXT
    
    ' Close any service handles.
    IF lngHService <> %NULL THEN
    CloseServiceHandle lngHService
    END IF
    
    IF lngHSCManager <> %NULL THEN
    CloseServiceHandle lngHSCManager
    END IF
    
    END FUNCTION
    
    ' Start up the service
    FUNCTION InitService() AS LONG
    DIM lngRet AS LONG
    DIM ID AS LONG
    DIM udtSTE AS SERVICE_TABLE_ENTRY
    DIM lpThreadAttributes AS SECURITY_ATTRIBUTES
    
    ' Start the main thread for this service
    glnghServiceThread = CreateThread(lpThreadAttributes, 0, CODEPTR(ServiceThread), 0, 0, ID)
    
    ' Did the thread start OK
    IF glnghServiceThread = 0 THEN
    FUNCTION = %FALSE
    EXIT FUNCTION
    ELSE
    ' Set the global to running
    glngRunningService = %TRUE
    
    FUNCTION = %TRUE
    EXIT FUNCTION
    END IF
    
    END FUNCTION
    
    SUB PauseService()
    
    ' Set the global indicating that we are not paused
    glngPauseService = %TRUE
    
    ' Let er rip
    SuspendThread glnghServiceThread
    
    END SUB
    
    SUB ResumeService()
    
    ' Set the global indicating that we are not paused
    glngPauseService = %FALSE
    
    ' Let er rip
    ResumeThread glnghServiceThread
    
    END SUB
    
    SUB StopService()
    
    ' Set the global flag indicating that the service is not running
    glngRunningService = %FALSE
    
    ' Set the event so the service will stop
    SetEvent glnghTerminateEvent
    
    END SUB
    
    FUNCTION SendStatus(BYVAL CurrentStatus AS DWORD, BYVAL ExitCode AS DWORD, ServiceSpecificExitCode AS LONG, BYVAL Checkpoint AS DWORD, BYVAL WaitHint AS DWORD) AS LONG
    DIM udtSS AS SERVICE_STATUS
    
    ' Reset the global service status value.
    gdwdServiceStatus = CurrentStatus
    
    ' Setup the UDT.
    udtSS.dwServiceType = %SERVICE_WIN32_OWN_PROCESS
    udtSS.dwCurrentState = CurrentStatus
    
    ' If we are the process of starting, then don't accept control events
    IF CurrentStatus = %SERVICE_START_PENDING THEN
    udtSS.dwControlsAccepted = 0
    ELSE
    ' Take what was given
    udtSS.dwControlsAccepted = %SERVICE_ACCEPT_STOP + %SERVICE_ACCEPT_PAUSE_CONTINUE + %SERVICE_ACCEPT_SHUTDOWN
    END IF
    
    ' If a specific ServiceSpecificExitCode is defined, setup the Win32 exit code properly
    IF ServiceSpecificExitCode = 0 THEN
    udtSS.dwWin32ExitCode = ExitCode
    ELSE
    udtSS.dwWin32ExitCode = %ERROR_SERVICE_SPECIFIC_ERROR
    END IF
    
    ' Specific Exit Code
    udtSS.dwServiceSpecificExitCode = ServiceSpecificExitCode
    
    udtSS.dwCheckPoint = Checkpoint
    udtSS.dwWaitHint = WaitHint
    
    IF SetServiceStatus(gdwdHStatus, udtSS) = 0 THEN
    ' Something went wrong so stop the service
    StopService
    FUNCTION = %FALSE
    ELSE
    FUNCTION = %TRUE
    END IF
    
    END FUNCTION
    
    SUB ServiceMain(BYVAL dwArgs AS DWORD, BYVAL lpszArgv AS DWORD)
    LOCAL Result AS LONG
    LOCAL lpEventAttributes AS SECURITY_ATTRIBUTES
    
    ' Register with the SCM
    gdwdHStatus = RegisterServiceCtrlHandler(gascServiceName, CODEPTR(Handler))
    ' Did it work
    IF gdwdHStatus = 0 THEN
    ' No, so terminate
    Terminate GetLastError()
    EXIT SUB
    END IF
    
    ' Service has been registered and startup is pending
    IF ISFALSE SendStatus(%SERVICE_START_PENDING, %NO_ERROR, 0, 1, 5000) THEN
    Terminate GetLastError()
    EXIT SUB
    END IF
    
    ' Create the termination event
    glnghTerminateEvent = CreateEvent(lpEventAttributes, %TRUE, %FALSE, "")
    IF glnghTerminateEvent = 0 THEN
    Terminate GetLastError()
    EXIT SUB
    END IF
    
    ' Service startup is still pending
    IF ISFALSE SendStatus(%SERVICE_START_PENDING, %NO_ERROR, 0, 2, 1000) THEN
    Terminate GetLastError()
    EXIT SUB
    END IF
    
    ' Start the service
    Result = InitService()
    IF ISFALSE Result THEN
    ' Oops
    Terminate GetLastError()
    EXIT SUB
    END IF
    
    ' Service is now running
    IF ISFALSE SendStatus(%SERVICE_RUNNING, %NO_ERROR, 0, 0, 0) THEN
    Terminate GetLastError()
    EXIT SUB
    END IF
    
    ' Wait for the signal to end
    WaitForSingleObject glnghTerminateEvent, %INFINITE
    
    Terminate 0
    
    EXIT SUB
    
    END SUB
    
    SUB Terminate(ErrCode AS DWORD)
    
    ' If the Terminate Event has already been created then destroy it
    IF glnghTerminateEvent <> 0 THEN
    CloseHandle glnghTerminateEvent
    END IF
    
    IF gdwdHStatus <> 0 THEN
    ' Send a message to the SCM and tell them that we are stopping
    SendStatus %SERVICE_STOPPED, ErrCode, 0&, 0&, 0&
    END IF
    
    ' If the thread has started, then kill it
    IF glnghServiceThread <> 0 THEN
    ' Not normally here in a service
    ' However, this program was written as an executable first and converted into a service
    ' There is a global hWindow that is set in the WINMAINX when we start
    ' We need to destroy the window so the WINMAINX will come back to us
    ' Is it a valid window
    IF IsWindow(hWindow) THEN
    ' Yes so destroy it
    DestroyWindow hWindow
    ' This will cause the Message Loop in your WINMAINX to exit and return
    END IF
    
    ' Close the thread
    CloseHandle glnghServiceThread
    END IF
    
    END SUB
    
    SUB Handler(BYVAL ControlValue AS DWORD)
    ' This procedure (by its' name) handles all service requests.
    LOCAL Result AS LONG
    
    ON ERROR RESUME NEXT
    
    ' There is no Start option because the ServiceMain takes care of starting the service
    SELECT CASE ControlValue
    CASE %SERVICE_CONTROL_STOP
    ' Set the global Status
    gdwdServiceState = %SERVICE_STOP_PENDING
    
    ' Tell the SCM that we are stopping
    SendStatus %SERVICE_STOP_PENDING, %NO_ERROR, 0&, 1, 5000
    
    ' Stop the service
    StopService
    CASE %SERVICE_CONTROL_PAUSE
    ' Are we running and not paused
    IF (ISTRUE glngRunningService) AND (ISFALSE glngPauseService) THEN
    ' Tell the SCM that we are pausing
    SendStatus %SERVICE_PAUSE_PENDING, %NO_ERROR, 0, 1, 1000
    
    ' Pause it
    PauseService
    
    ' Set the current state
    gdwdServiceState = %SERVICE_PAUSED
    END IF
    CASE %SERVICE_CONTROL_CONTINUE
    ' Are we running and paused
    IF (ISTRUE glngRunningService) AND (ISTRUE glngPauseService) THEN
    ' Tell the SCM that we are un pausing
    SendStatus %SERVICE_CONTINUE_PENDING, %NO_ERROR, 0, 1, 1000
    
    ' Resume the service
    ResumeService
    
    ' Set the current state
    gdwdServiceState = %SERVICE_RUNNING
    END IF
    CASE %SERVICE_CONTROL_INTERROGATE
    ' Don't need to do anything
    ' We will send the current status below
    CASE %SERVICE_CONTROL_SHUTDOWN
    ' We don't do anything with a shutdown
    EXIT SUB
    END SELECT
    
    ' Tell the SCM the new status
    SendStatus gdwdServiceState, %NO_ERROR, 0, 0, 0
    
    END SUB
    
    FUNCTION ServiceThread(ID AS LONG) EXPORT AS LONG
    LOCAL Result AS LONG
    LOCAL Msg AS tagMsg
    
    ' Run until we are killed
    Result = WINMAINX()
    ' I had written a program and compiled it as an exe
    ' To make it a service, I just renamed the WINMAIN to WINMAINX and then
    ' call it like you see above
    
    END FUNCTION
    Attached Files

    Leave a comment:


  • Paul Purvis
    replied
    i changed the name of this version of the above to "proxypluslogkeeper"

    'this program runs only as a service
    'place the exe in the windows directory
    'to install the program enter "proxypluslogkeeper install" then reboot
    'to remove the program enter "proxypluslogkeeper uninstall" then reboot
    'if you use stop the service with console command "net stop proxyplus log keeper" you cannot
    ' restart the service using the "net start" command, you have to reboot to restart the service
    '

    this part of this program that makes it a service came from the forums at this location

    http://powerbasic.com/support/pbforu...ead.php?t=3889


    i have used this code before to create a service and it is working just great
    even if i cannot stop and start the service with the "net stop" and "net start" commands. yes the service can be stopped with "net stop" but not restarted, a reboot has to restart the service.
    from the task manager this program cannot be stopped which i do like because i do not want users stopping the service.

    i used the same code to create the service to the program called logwostm, which you can find in this forum.

    The exe can be patched to edit some file naming conventions as i left plenty of space in the original defining of the variables.
    This program still does not access the registry to get values set by the proxy+(proxyplus) server program

    If anybody wants to start another thread to discuss this program that is fine with me.

    If you are going to change the naming of the program and/or service, pay attention to the following two lines in the source of the program
    Code:
    gascServiceName          = trim$("ProxyPlusLogKeeper Service              ")
    gascServiceDisplayName = trim$("ProxyPlus Log Keeper                       ")
    Code:
    'proxypluslogkeeper.bas
    'pbwin 9.01 complier
    'this program runs only as a service
    'place the exe in the windows directory
    'to install the program enter "proxypluslogkeeper install" then reboot
    'to remove  the program enter "proxypluslogkeeper uninstall" then reboot
    'if you use stop the service with console command "net stop proxyplus log keeper" you cannot
    '         restart the service using the "net start" command, you have to reboot to restart the service
    '
    
    '
    ' this program reads log files from proxy+ (proxyplus) version 3.09 build 264
    ' then saves them else where on the hard drive
    ' the directory  created is "\systemplus" that is hidden in the root directory
    ' a subdirectory in the directory  \systemplus is created called proxyplus where
    ' log files are placed by date
    'ex "c:\systemplus\proxyplus\20091231"
    'files names are incremented to keep from losing log data from the proxyplus
    'after the log files from proxyplus are read and new ones created under the
    '"\systemplus\proxyplus" directory, the original log files are deleted.
    
    'you can set the number of logs to create and the number of days in each period before logs are archieved(rolledover)
    
    'there seems to be a maximum of 31 periods
    '
    'proxyplus keeps data sinformation in the registry
    'there is information in the registry but at this time this program does not read or make changes to the registry
    
    
    
    
    #COMPILE EXE  "ProxyPlusLogKeeper.exe"
    #DIM ALL
    #REGISTER NONE
    #INCLUDE "WIN32API.INC"
    
    GLOBAL hWindow AS LONG ' You should set this in your WINMAINX to the handle
    ' of your main window.
    
    FUNCTION winmainx() AS LONG
    
     DIM pathlogdirectory     AS STRING
     DIM newpathlogdirectory  AS STRING
     DIM todaysdate           AS STRING
     DIM fileexist            AS LONG
     DIM filenametemp         AS STRING
     DIM homedrive            AS STRING
     DIM ProgramFiles         AS STRING
     DIM temp                 AS STRING
    
     DIM I AS LONG
     DIM J AS LONG
     DIM K AS LONG
     DIM L AS LONG
     DIM M AS LONG
    
     DIM proxypluslogdirectory       AS STRING
     DIM proxypluslogfilenamebase(6) AS STRING
     DIM proxypluslogfilenameext     AS STRING
     DIM proxypluslogfilename        AS STRING
     DIM newlogfilename              AS STRING
     DIM fileno1                     AS LONG
     DIM fileno2                     AS LONG
     DIM filecontents                AS STRING
    
    
    SLEEP 180000  ' wait 3 minutes before the program starts trying to reading the log files after a bootup or restart
    
    
    
    
    
    ' some of these variables are in the registry, but this program does not read the registry
    
        proxypluslogdirectory=         TRIM$("C:\Program Files\ProxyPlus\Logs                                                               ")
        proxypluslogfilenamebase(1)=   TRIM$("AccessLog_                                                                                    ")
        proxypluslogfilenamebase(2)=   TRIM$("ProxyLog_                                                                                     ")
        proxypluslogfilenamebase(3)=   TRIM$("DialLog_                                                                                      ")
        proxypluslogfilenamebase(4)=   TRIM$("MailLog_                                                                                      ")
        proxypluslogfilenamebase(5)=   TRIM$("ErrLog_                                                                                       ")
        proxypluslogfilenamebase(6)=   TRIM$("SecLog_                                                                                       ")
        proxypluslogfilenameext    =   TRIM$(".TXT                                                                                          ")
    
    
    
        startprocessinglogfiles:
        
        'wait 4 minutes if the time is between 2 minutes till midnight and 5 minutes after midnight
        'this gives the proxyplus service time to create new log files that are suppose to be create automatically
        '    around midnight on any given day according to the period setup in proxyplus
        I = TIMER
        IF I > 86280 OR I < 300 THEN SLEEP 240000:GOTO startprocessinglogfiles
                                               
                          
        homedrive=ENVIRON$("HOMEDRIVE")
        ProgramFiles=ENVIRON$("ProgramFiles")
    
        pathlogdirectory=homedrive+"\systemplus"
        MKDIR pathlogdirectory
    
        SETATTR pathlogdirectory, %HIDDEN + %SYSTEM + %NORMAL
    
        pathlogdirectory=pathlogdirectory+"\proxypluslogs"
        MKDIR pathlogdirectory
    
        logitagain:
        todaysdate=DATE$
    '   next line will store all logs in a directory by a specfic year and month
        newpathlogdirectory=pathlogdirectory+"\"+MID$(todaysdate,7,4)+MID$(todaysdate,1,2)
    '   unremarking the next line will store all logs in a directory by a specfic year, month and day
    '    newpathlogdirectory=pathlogdirectory+"\"+MID$(todaysdate,7,4)+MID$(todaysdate,1,2)+MID$(todaysdate,4,2)
    
        MKDIR newpathlogdirectory
    
    
     ' find the file name with the highest number in it, this will be the oldest log file
        M = 0&
        L = 0&
        FOR I = 0& TO 99999&
        IF (I MOD 10&)= 0& THEN SLEEP 30&
        IF L = 300& THEN EXIT FOR  ' this stop the program if more than 50 straight log files are not found
                               ' proxyplus seems to limit log files to a maximum of 31 logfiles on a rollover basis
        FOR K = 1& TO 6&
        filenametemp=proxypluslogdirectory+"\"+proxypluslogfilenamebase(k)+TRIM$(STR$(I))+proxypluslogfilenameext
        IF NOT ISFILE(filenametemp)THEN INCR L:ITERATE
        IF ISFILE(filenametemp)THEN M=I:L=0&:ITERATE:ITERATE
        NEXT K
        NEXT I
       IF M = 0& THEN GOTO endprocessing
    
    
       FOR I = M TO 0&  STEP -1&
        FOR K = 1& TO 6&
        filecontents = ""
        filenametemp = proxypluslogdirectory+"\"+proxypluslogfilenamebase(K)+TRIM$(STR$(I))+proxypluslogfilenameext
        IF NOT ISFILE(filenametemp)THEN ITERATE
         TRY
        fileno1 = FREEFILE
         OPEN filenametemp FOR BINARY ACCESS READ LOCK WRITE AS #fileno1
         CATCH
         CLOSE #fileno1
         EXIT TRY
         FINALLY
           IF LOF(fileno1) = 0& THEN
               CLOSE #fileno1
               KILL filenametemp
               ITERATE
           EXIT TRY
           END IF
                filecontents = ""
         GET$ fileno1, LOF(fileno1),filecontents
         CLOSE #fileno1
         L = 0&
        END TRY
    
    
    
       FOR J=1& TO 9999999&
          newlogfilename = newpathlogdirectory+"\"+proxypluslogfilenamebase(K)+TRIM$(STR$(J))+proxypluslogfilenameext
    
          IF ISFILE(newlogfilename)THEN ITERATE
    
          fileno2 = FREEFILE
          TRY
            OPEN newlogfilename FOR BINARY ACCESS READ WRITE LOCK READ WRITE AS #fileno2
          CATCH
            CLOSE #fileno2
            EXIT TRY
          FINALLY
            PUT$ fileno2, filecontents
            CLOSE #fileno2
            KILL filenametemp
            EXIT FOR
          END TRY
        NEXT J
    
        NEXT K
    
        NEXT I
    
    endprocessing:
        SLEEP 60000 'wait 60 seconds before scanning for new log files
        GOTO startprocessinglogfiles
    
    END FUNCTION
    
    
    
    ' ---------------------
    ' CONSTANT DECLARATIONS
    ' ---------------------
    %EVENTLOG_AUDIT_FAILURE = 16
    %EVENTLOG_AUDIT_SUCCESS = 8
    %EVENTLOG_ERROR_TYPE = 1
    %EVENTLOG_INFORMATION_TYPE = 4
    %EVENTLOG_SUCCESS = 0
    %EVENTLOG_WARNING_TYPE = 2
    
    ' This constant is used to set logging status.
    %EVENTLOG_LEVEL = %EVENTLOG_AUDIT_FAILURE + %EVENTLOG_AUDIT_SUCCESS + %EVENTLOG_ERROR_TYPE + _
    %EVENTLOG_INFORMATION_TYPE + %EVENTLOG_SUCCESS + %EVENTLOG_WARNING_TYPE
    %SERVICE_WIN32_OWN_PROCESS = &H10&
    %SERVICE_WIN32_SHARE_PROCESS = &H20&
    %SERVICE_WIN32 = %SERVICE_WIN32_OWN_PROCESS + %SERVICE_WIN32_SHARE_PROCESS
    %SERVICE_DEMAND_START = &H3&
    %SERVICE_ERROR_NORMAL = &H1&
    %WAIT_OBJECT_0 = 0&
    
    ' -------------------------
    ' FUNCTION/SUB DECLARATIONS
    ' -------------------------
    DECLARE FUNCTION Install() AS LONG
    DECLARE FUNCTION InitService() AS LONG
    DECLARE FUNCTION Uninstall() AS LONG
    DECLARE FUNCTION WinErrorMessage(BYVAL ErrNumber AS LONG) AS STRING
    DECLARE FUNCTION SendStatus(BYVAL CurrentStatus AS DWORD, BYVAL ExitCode AS DWORD, ServiceSpecificExitCode AS LONG, BYVAL Checkpoint AS DWORD, BYVAL WaitHint AS DWORD) AS LONG
    DECLARE FUNCTION ServiceThread(ID AS LONG) AS LONG
    DECLARE SUB Handler(BYVAL ControlValue AS DWORD)
    DECLARE SUB LogNTEvent(Message AS STRING, LogCode AS LONG)
    DECLARE SUB ServiceMain(BYVAL dwArgs AS DWORD, BYVAL lpszArgv AS DWORD)
    DECLARE SUB Terminate(ErrCode AS DWORD)
    DECLARE SUB StopService()
    DECLARE SUB PauseService()
    DECLARE SUB ResumeService()
    
    ' ----------------
    ' GLOBAL VARIABLES
    ' ----------------
    GLOBAL gascServiceName AS ASCIIZ * 8
    GLOBAL gascServiceDisplayName AS ASCIIZ * 256
    GLOBAL gdwdhStatus AS DWORD
    GLOBAL gdwdServiceState AS DWORD
    GLOBAL gdwdServiceStatus AS DWORD
    GLOBAL glnghInstance AS LONG
    GLOBAL glnghPrvInst AS LONG
    GLOBAL glnghTerminateEvent AS LONG
    GLOBAL glnghServiceThread AS LONG
    GLOBAL glngThreadResult AS LONG
    GLOBAL glngRunningService AS LONG
    GLOBAL glngPauseService AS LONG
    GLOBAL glngCmdShow AS LONG
    GLOBAL ptrCmdLine AS ASCIIZ PTR
    
    ' FUNCTION: WinMain
    FUNCTION WINMAIN(BYVAL hCurInst AS LONG, BYVAL hPrvInst AS LONG, BYVAL CmdLine AS ASCIIZ PTR, BYVAL CmdShow AS LONG) EXPORT AS LONG
    'FUNCTION PBMAIN () AS LONG
    
    DIM Result AS LONG
    DIM CmdParm AS STRING
    DIM udtSTE AS SERVICE_TABLE_ENTRY
    
    ' Set the service name and display name here.
    gascServiceName          = trim$("ProxyPlusLogKeeper Service              ")
    gascServiceDisplayName = trim$("ProxyPlus Log Keeper                       ")
    glngHInstance = hCurInst
    glnghPrvInst = hPrvInst
    glngCmdShow = CmdShow
    ptrCmdLine = CmdLine
    
    ' Get the Command Line parms
    CmdParm = UCASE$(EXTRACT$(@CmdLine, ANY " ,." + CHR$(9)))
    ' Get rid of any seperators
    CmdParm = REMOVE$(CmdParm, ANY "-\/")
    
    ' If the exe was executed with the Install parm then we just install and quit
    IF CmdParm = "INSTALL" THEN
    ' Install the service.
    Result = Install()
    IF ISFALSE Result THEN
    MSGBOX "An error occured while trying to install this service
    END IF
    ' If the exe was executed with the UnInstall parm then we just uninstall and quit
    ELSEIF CmdParm = "UNINSTALL" THEN
    ' Uninstall the service.
    Result = Uninstall()
    IF ISFALSE Result THEN
    MSGBOX "An error occured while trying to uninstall this service
    END IF
    
    ' No parms were given that we want so start up as service
    ' This should only happen when the service control manager starts us
    ELSE
    
    udtSTE.lpServiceName = VARPTR(gascServiceName)
    udtSTE.lpServiceProc = CODEPTR(ServiceMain)
    
    Result = StartServiceCtrlDispatcher(udtSTE)
    
    IF Result = 0 THEN
    ExitProcess GetLastError()
    END IF
    
    END IF
    
    FUNCTION = 0
    
    END FUNCTION
    
    
    
    
    
    
    ' Install the service into windows
    FUNCTION Install() AS LONG
    DIM dwdRet AS DWORD
    DIM lngDQEnd AS LONG
    DIM lngDQStart AS LONG
    DIM lngHSCManager AS LONG
    DIM lngHService AS LONG
    DIM ascEXE AS ASCIIZ * %MAX_PATH
    
    ON ERROR GOTO Error_Install
    
    ' Assume a failure for now.
    FUNCTION = %FALSE
    
    lngHSCManager = OpenSCManager(BYVAL %NULL, BYVAL %NULL, %SC_MANAGER_CREATE_SERVICE)
    
    IF lngHSCManager <> %NULL THEN
    ' OK, we have a handle to the SCM.
    ' Get the full EXE file path.
    dwdRet = GetModuleFileName(glngHInstance, ascEXE, %MAX_PATH)
    IF dwdRet <> 0 THEN
    ' Install the service.
    lngHService = CreateService(lngHSCManager, gascServiceName, gascServiceDisplayName, _
    %SERVICE_ALL_ACCESS, %SERVICE_WIN32_OWN_PROCESS, _
    %SERVICE_AUTO_START, %SERVICE_ERROR_NORMAL, _
    ascEXE, BYVAL %NULL, BYVAL %NULL, _
    BYVAL %NULL, BYVAL %NULL, BYVAL %NULL)
    ' Close any service handles.
    IF lngHService <> %NULL THEN
    ' Success!
    FUNCTION = %TRUE
    CloseServiceHandle lngHService
    END IF
    CloseServiceHandle lngHSCManager
    END IF
    END IF
    
    FUNCTION = %TRUE
    
    EXIT FUNCTION
    
    Error_Install:
    
    FUNCTION = -1& * ERR
    
    ON ERROR RESUME NEXT
    
    ' Close any service handles.
    IF lngHService <> %NULL THEN
    CALL CloseServiceHandle(lngHService)
    END IF
    
    IF lngHSCManager <> %NULL THEN
    CALL CloseServiceHandle(lngHSCManager)
    END IF
    
    END FUNCTION
    
    ' Unistall the service
    FUNCTION Uninstall() AS LONG
    DIM lngHSCManager AS LONG
    DIM lngHService AS LONG
    
    ON ERROR GOTO Error_Uninstall
    
    ' Assume a failure for now.
    FUNCTION = %FALSE
    
    lngHSCManager = OpenSCManager(BYVAL %NULL, BYVAL %NULL, %SC_MANAGER_CREATE_SERVICE)
    
    IF lngHSCManager <> %NULL THEN
    ' OK, we have a handle to the SCM.
    ' Now open our service.
    lngHService = OpenService(lngHSCManager, gascServiceName, %SERVICE_ALL_ACCESS)
    IF lngHService <> %NULL THEN
    ' Delete the service.
    IF DeleteService(lngHService) <> %NULL THEN
    ' Success!
    FUNCTION = %TRUE
    END IF
    CloseServiceHandle lngHService
    END IF
    CloseServiceHandle lngHSCManager
    END IF
    
    EXIT FUNCTION
    
    Error_Uninstall:
    
    FUNCTION = -1& * ERR
    
    ON ERROR RESUME NEXT
    
    ' Close any service handles.
    IF lngHService <> %NULL THEN
    CloseServiceHandle lngHService
    END IF
    
    IF lngHSCManager <> %NULL THEN
    CloseServiceHandle lngHSCManager
    END IF
    
    END FUNCTION
    
    ' Start up the service
    FUNCTION InitService() AS LONG
    DIM lngRet AS LONG
    DIM ID AS LONG
    DIM udtSTE AS SERVICE_TABLE_ENTRY
    DIM lpThreadAttributes AS SECURITY_ATTRIBUTES
    
    ' Start the main thread for this service
    glnghServiceThread = CreateThread(lpThreadAttributes, 0, CODEPTR(ServiceThread), 0, 0, ID)
    
    ' Did the thread start OK
    IF glnghServiceThread = 0 THEN
    FUNCTION = %FALSE
    EXIT FUNCTION
    ELSE
    ' Set the global to running
    glngRunningService = %TRUE
    
    FUNCTION = %TRUE
    EXIT FUNCTION
    END IF
    
    END FUNCTION
    
    SUB PauseService()
    
    ' Set the global indicating that we are not paused
    glngPauseService = %TRUE
    
    ' Let er rip
    SuspendThread glnghServiceThread
    
    END SUB
    
    SUB ResumeService()
    
    ' Set the global indicating that we are not paused
    glngPauseService = %FALSE
    
    ' Let er rip
    ResumeThread glnghServiceThread
    
    END SUB
    
    SUB StopService()
    
    ' Set the global flag indicating that the service is not running
    glngRunningService = %FALSE
    
    ' Set the event so the service will stop
    SetEvent glnghTerminateEvent
    
    END SUB
    
    FUNCTION SendStatus(BYVAL CurrentStatus AS DWORD, BYVAL ExitCode AS DWORD, ServiceSpecificExitCode AS LONG, BYVAL Checkpoint AS DWORD, BYVAL WaitHint AS DWORD) AS LONG
    DIM udtSS AS SERVICE_STATUS
    
    ' Reset the global service status value.
    gdwdServiceStatus = CurrentStatus
    
    ' Setup the UDT.
    udtSS.dwServiceType = %SERVICE_WIN32_OWN_PROCESS
    udtSS.dwCurrentState = CurrentStatus
    
    ' If we are the process of starting, then don't accept control events
    IF CurrentStatus = %SERVICE_START_PENDING THEN
    udtSS.dwControlsAccepted = 0
    ELSE
    ' Take what was given
    udtSS.dwControlsAccepted = %SERVICE_ACCEPT_STOP + %SERVICE_ACCEPT_PAUSE_CONTINUE + %SERVICE_ACCEPT_SHUTDOWN
    END IF
    
    ' If a specific ServiceSpecificExitCode is defined, setup the Win32 exit code properly
    IF ServiceSpecificExitCode = 0 THEN
    udtSS.dwWin32ExitCode = ExitCode
    ELSE
    udtSS.dwWin32ExitCode = %ERROR_SERVICE_SPECIFIC_ERROR
    END IF
    
    ' Specific Exit Code
    udtSS.dwServiceSpecificExitCode = ServiceSpecificExitCode
    
    udtSS.dwCheckPoint = Checkpoint
    udtSS.dwWaitHint = WaitHint
    
    IF SetServiceStatus(gdwdHStatus, udtSS) = 0 THEN
    ' Something went wrong so stop the service
    StopService
    FUNCTION = %FALSE
    ELSE
    FUNCTION = %TRUE
    END IF
    
    END FUNCTION
    
    SUB ServiceMain(BYVAL dwArgs AS DWORD, BYVAL lpszArgv AS DWORD)
    LOCAL Result AS LONG
    LOCAL lpEventAttributes AS SECURITY_ATTRIBUTES
    
    ' Register with the SCM
    gdwdHStatus = RegisterServiceCtrlHandler(gascServiceName, CODEPTR(Handler))
    ' Did it work
    IF gdwdHStatus = 0 THEN
    ' No, so terminate
    Terminate GetLastError()
    EXIT SUB
    END IF
    
    ' Service has been registered and startup is pending
    IF ISFALSE SendStatus(%SERVICE_START_PENDING, %NO_ERROR, 0, 1, 5000) THEN
    Terminate GetLastError()
    EXIT SUB
    END IF
    
    ' Create the termination event
    glnghTerminateEvent = CreateEvent(lpEventAttributes, %TRUE, %FALSE, "")
    IF glnghTerminateEvent = 0 THEN
    Terminate GetLastError()
    EXIT SUB
    END IF
    
    ' Service startup is still pending
    IF ISFALSE SendStatus(%SERVICE_START_PENDING, %NO_ERROR, 0, 2, 1000) THEN
    Terminate GetLastError()
    EXIT SUB
    END IF
    
    ' Start the service
    Result = InitService()
    IF ISFALSE Result THEN
    ' Oops
    Terminate GetLastError()
    EXIT SUB
    END IF
    
    ' Service is now running
    IF ISFALSE SendStatus(%SERVICE_RUNNING, %NO_ERROR, 0, 0, 0) THEN
    Terminate GetLastError()
    EXIT SUB
    END IF
    
    ' Wait for the signal to end
    WaitForSingleObject glnghTerminateEvent, %INFINITE
    
    Terminate 0
    
    EXIT SUB
    
    END SUB
    
    SUB Terminate(ErrCode AS DWORD)
    
    ' If the Terminate Event has already been created then destroy it
    IF glnghTerminateEvent <> 0 THEN
    CloseHandle glnghTerminateEvent
    END IF
    
    IF gdwdHStatus <> 0 THEN
    ' Send a message to the SCM and tell them that we are stopping
    SendStatus %SERVICE_STOPPED, ErrCode, 0&, 0&, 0&
    END IF
    
    ' If the thread has started, then kill it
    IF glnghServiceThread <> 0 THEN
    ' Not normally here in a service
    ' However, this program was written as an executable first and converted into a service
    ' There is a global hWindow that is set in the WINMAINX when we start
    ' We need to destroy the window so the WINMAINX will come back to us
    ' Is it a valid window
    IF IsWindow(hWindow) THEN
    ' Yes so destroy it
    DestroyWindow hWindow
    ' This will cause the Message Loop in your WINMAINX to exit and return
    END IF
    
    ' Close the thread
    CloseHandle glnghServiceThread
    END IF
    
    END SUB
    
    SUB Handler(BYVAL ControlValue AS DWORD)
    ' This procedure (by its' name) handles all service requests.
    LOCAL Result AS LONG
    
    ON ERROR RESUME NEXT
    
    ' There is no Start option because the ServiceMain takes care of starting the service
    SELECT CASE ControlValue
    CASE %SERVICE_CONTROL_STOP
    ' Set the global Status
    gdwdServiceState = %SERVICE_STOP_PENDING
    
    ' Tell the SCM that we are stopping
    SendStatus %SERVICE_STOP_PENDING, %NO_ERROR, 0&, 1, 5000
    
    ' Stop the service
    StopService
    CASE %SERVICE_CONTROL_PAUSE
    ' Are we running and not paused
    IF (ISTRUE glngRunningService) AND (ISFALSE glngPauseService) THEN
    ' Tell the SCM that we are pausing
    SendStatus %SERVICE_PAUSE_PENDING, %NO_ERROR, 0, 1, 1000
    
    ' Pause it
    PauseService
    
    ' Set the current state
    gdwdServiceState = %SERVICE_PAUSED
    END IF
    CASE %SERVICE_CONTROL_CONTINUE
    ' Are we running and paused
    IF (ISTRUE glngRunningService) AND (ISTRUE glngPauseService) THEN
    ' Tell the SCM that we are un pausing
    SendStatus %SERVICE_CONTINUE_PENDING, %NO_ERROR, 0, 1, 1000
    
    ' Resume the service
    ResumeService
    
    ' Set the current state
    gdwdServiceState = %SERVICE_RUNNING
    END IF
    CASE %SERVICE_CONTROL_INTERROGATE
    ' Don't need to do anything
    ' We will send the current status below
    CASE %SERVICE_CONTROL_SHUTDOWN
    ' We don't do anything with a shutdown
    EXIT SUB
    END SELECT
    
    ' Tell the SCM the new status
    SendStatus gdwdServiceState, %NO_ERROR, 0, 0, 0
    
    END SUB
    
    FUNCTION ServiceThread(ID AS LONG) EXPORT AS LONG
    LOCAL Result AS LONG
    LOCAL Msg AS tagMsg
    
    ' Run until we are killed
    Result = WINMAINX()
    ' I had written a program and compiled it as an exe
    ' To make it a service, I just renamed the WINMAIN to WINMAINX and then
    ' call it like you see above
    
    END FUNCTION
    Attached Files
    Last edited by Paul Purvis; 10 Sep 2009, 03:40 AM.

    Leave a comment:


  • Paul Purvis
    replied
    the current proxypluslog.exe
    this program is not set to run as a service.
    Attached Files
    Last edited by Paul Purvis; 10 Sep 2009, 01:04 AM.

    Leave a comment:


  • Archiving logs created by Proxy+ http proxy server

    here is my attempt to capture where my users are going on the internet using proxy+(proxyplus), a http proxy server that does some other services as well and it can run as a windows service.
    The version is 3.00 build no 264.

    The proxy+ server can archive a copy of the logs created up to 31 periods , you can set the period(interval of days or weeks, or something else).
    It calls this log rollover. The program keeps active logs opened and locked, so you can not access them unless the program is not running and we want it to always be running as a service. If any future version of this program does the same and increases the number of back logs higher that 31, this program will handle it.

    So i wrote this program to grab the archived(rolled over) logs and create logs elsewhere on the computer. After retrieving data inside the logs files, i then erase the original archived log file.

    There are six log files and this program saves all 6.

    I intend this program to be run as a windows service, but this program listing as is will not run as a service, you will have to just remove some remarked lines out.

    You can edit the program to save the files in directories of individual days, but i thought that was just too much so i changed it place logs into directories by year and month.

    I place some code in the program to speed up the process, where certain files do not exist. I just try to open files and do not view a directory to see what files are there.

    It would be nice to read the registry for some information, but i have not ever done that and i am looking into it now. It would be nice to not have this program hard code to where certain information is stored, but it will handle proxy+'s default setup.

    I have not tried this program on VISTA yet or even as a service yet, but it should work as a service, that is the next step.

    Proxy+ does not really increment the log numbers, it renames the oldest log file, the highest number. So a log file with a lower number is more current than a log file with a higher number.

    If the original log file has zero bytes in it(empty), then nothing is saved. That also means because there are 6 log files for the program, these logs are not saved as a set, so to say.

    While running this program, it would seem logical there are not going to be no more that 31x6 files in a single directory for one particular month, but this program will handle much much more than that if a user manually decides to rollover the log files to escape detection of where he goes on the internet.


    Code:
    proxypluslog.bas
    'pbwin 9.01 complier
    '
    ' this program reads log files from proxy+ (proxyplus) version 3.09 build 264
    ' then saves them else where on the hard drive
    ' the directory  created is "\systemplus" that is hidden in the root directory
    ' a subdirectory in the directory  \systemplus is created called proxyplus where
    ' log files are placed by date
    'ex "c:\systemplus\proxyplus\20091231"
    'files names are incremented to keep from losing log data from the proxyplus
    'after the log files from proxyplus are read and new ones created under the
    '"\systemplus\proxyplus" directory, the original log files are deleted.
    
    'you can set the number of logs to create and the number of days in each period before logs are archieved(rolledover)
    
    'there seems to be a maximum of 31 periods
    '
    'proxyplus keeps data information in the registry
    'there is information in the registry but at this time this program does not read or make changes to the registry
    
    
    
    
    #COMPILE EXE  "ProxyPlusLog.exe"
    #DIM ALL
    #REGISTER NONE
    #INCLUDE "WIN32API.INC"
    
    
    
    FUNCTION PBMAIN () AS LONG
        
     DIM pathlogdirectory     AS STRING
     DIM newpathlogdirectory  AS STRING
     DIM todaysdate           AS STRING
     DIM fileexist            AS LONG
     DIM filenametemp         AS STRING
     DIM homedrive            AS STRING
     DIM ProgramFiles         AS STRING
     DIM temp                 AS STRING
    
     DIM I AS LONG
     DIM J AS LONG
     DIM K AS LONG
     DIM L AS LONG
     DIM M AS LONG
     
     DIM proxypluslogdirectory       AS STRING
     DIM proxypluslogfilenamebase(6) AS STRING
     DIM proxypluslogfilenameext     AS STRING
     DIM proxypluslogfilename        AS STRING
     DIM newlogfilename              AS STRING
     DIM fileno1                     AS LONG
     DIM fileno2                     AS LONG
     DIM filecontents                AS STRING
     
    ' unremark the next line if you want to run this program as a service.
    'SLEEP 240000  '4 minutes before program starts
                                                          
    
    
    
    ' some of these variables are in the registry, but this program does not read the registry
    
        proxypluslogdirectory=  TRIM$("C:\Program Files\ProxyPlus\Logs                                                               ")
        proxypluslogfilenamebase(1)=   TRIM$("AccessLog_                                                                                    ")
        proxypluslogfilenamebase(2)=   TRIM$("ProxyLog_                                                                                    ")
        proxypluslogfilenamebase(3)=   TRIM$("DialLog_                                                                                    ")
        proxypluslogfilenamebase(4)=   TRIM$("MailLog_                                                                                    ")
        proxypluslogfilenamebase(5)=   TRIM$("ErrLog_                                                                                    ")
        proxypluslogfilenamebase(6)=   TRIM$("SecLog_                                                                                    ")
        proxypluslogfilenameext=     TRIM$(".TXT                                                                                          ")
    
    
    
    startprocessing:
    
        homedrive=ENVIRON$("HOMEDRIVE")
        ProgramFiles=ENVIRON$("ProgramFiles")
    
        pathlogdirectory=homedrive+"\systemplus"
        MKDIR pathlogdirectory
    
        SETATTR pathlogdirectory, %HIDDEN + %SYSTEM + %NORMAL
    
        pathlogdirectory=pathlogdirectory+"\proxypluslogs"
        MKDIR pathlogdirectory
    
        logitagain:
        todaysdate=DATE$
    '   next line will store all logs in a directory by a specfic year and month
        newpathlogdirectory=pathlogdirectory+"\"+MID$(todaysdate,7,4)+MID$(todaysdate,1,2)
    '   unremarking the next line will store all logs in a directory by a specfic year, month and day
    '    newpathlogdirectory=pathlogdirectory+"\"+MID$(todaysdate,7,4)+MID$(todaysdate,1,2)+MID$(todaysdate,4,2)
    
        MKDIR newpathlogdirectory
        
        
     ' find the file name with the highest number in it
        M = 0&
        L = 0&
        FOR I = 0& TO 99999&
        IF (I MOD 10&)= 0& THEN SLEEP 30&
        IF L = 300& THEN EXIT FOR  ' this stop the program if more than 50 straight log files are not found
                               ' proxyplus seems to limit log files to a maximum of 31 logfiles on a rollover basis
        FOR K = 1& TO 6&
        filenametemp=proxypluslogdirectory+"\"+proxypluslogfilenamebase(k)+TRIM$(STR$(I))+proxypluslogfilenameext
        IF NOT ISFILE(filenametemp)THEN INCR L:ITERATE
        IF ISFILE(filenametemp)THEN M=I:L=0&:ITERATE:ITERATE
        NEXT K
        NEXT I
       IF M = 0& THEN GOTO endprocessing
       
    
       FOR I = M TO 0&  STEP -1&
        FOR K = 1& TO 6&
        filecontents = ""
        filenametemp = proxypluslogdirectory+"\"+proxypluslogfilenamebase(K)+TRIM$(STR$(I))+proxypluslogfilenameext
        IF NOT ISFILE(filenametemp)THEN ITERATE
         TRY
        fileno1 = FREEFILE
         OPEN filenametemp FOR BINARY ACCESS READ LOCK WRITE AS #fileno1
         CATCH
         CLOSE #fileno1
         EXIT TRY
         FINALLY
           IF LOF(fileno1) = 0& THEN
               CLOSE #fileno1
               KILL filenametemp
               ITERATE
           EXIT TRY
           END IF
                filecontents = ""
         GET$ fileno1, LOF(fileno1),filecontents
         CLOSE #fileno1
         L = 0&
        END TRY
    
    
    
       FOR J=1& TO 9999999&
          newlogfilename = newpathlogdirectory+"\"+proxypluslogfilenamebase(K)+TRIM$(STR$(J))+proxypluslogfilenameext
    
          IF ISFILE(newlogfilename)THEN ITERATE
    
          fileno2 = FREEFILE
          TRY
            OPEN newlogfilename FOR BINARY ACCESS READ WRITE LOCK READ WRITE AS #fileno2
          CATCH
            CLOSE #fileno2
            EXIT TRY
          FINALLY
            PUT$ fileno2, filecontents
            CLOSE #fileno2
            KILL filenametemp
            EXIT FOR
          END TRY
        NEXT J
        
        NEXT K
     
        IF (I MOD 10&) = 0& THEN SLEEP 20
    
        NEXT I
        
    endprocessing:
       TEMP = UCASE$(COMMAND$)
       REPLACE " " WITH "" IN TEMP
       IF INSTR(TEMP,"QUIT") THEN
           EXIT FUNCTION
           ELSE
    'unremark the next two lines if running this program as a windows service
    '       SLEEP 60000 'capture the logs files once every every 60 seconds
    '       GOTO startprocessing
       END IF
       
    END FUNCTION
    Attached Files
    Last edited by Paul Purvis; 9 Sep 2009, 08:29 PM.
Working...
X