Automatic proxy setting for regify client

From regify WIKI
Revision as of 10:25, 30 October 2013 by Regify (talk | contribs) (Created page with 'It allows to automatically set the proxy server settings of the regify client depending on the current active IP address. == Remarks == * You need to configure this script at th…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

It allows to automatically set the proxy server settings of the regify client depending on the current active IP address.

Remarks

  • You need to configure this script at the first lines.
  • Dont forget to set LocalNet to the beginning of the network that needs proxy (eg "10.1.1" or "192.168" etc.).
  • We suggest to save this as setRegifyProxy.vbs or similar directly to the desktop. The .vbs extension allows execution by simple doubleclick.
  • You are allowed to adapt this script to your needs.

The script

In order to set the regify client proxy settings regarding the current IP address, the following VBScript may get used:

Dim LocalNet, Proxy, ProxyUser, ProxyPass, INIFile, Loud

' --------- Start Config ---------
LocalNet  = "192.168.12"
Proxy     = "shuttle.lu.regify.com:3128" ' Name:Port
ProxyUser = "pUser"
ProxyPass = "pPass"
Loud      = True ' If some message appears or not (True/False)
' --------- End Config ---------

Dim NIC1, Nic, StrIP, CompName, Ini, NewIni, ValueExp, Mode
Dim objFSO, copyFile, vSystemDrive
' Get some RegExp engine
Set objRegEx = New RegExp
objRegEx.IgnoreCase = False
objRegEx.Global = True
ValueExp = ".*\r\n"
' Get regify client ini file
Set WshShell = WScript.CreateObject("Wscript.Shell")
vAPPDATA = WshShell.ExpandEnvironmentStrings("%APPDATA%")
INIFile = vAPPDATA & "\regify\regify_client.ini"

' Get IP Address of current active NIC
Set NIC1 = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
For Each Nic in NIC1
  If Nic.IPEnabled Then
    StrIP = Nic.IPAddress(i)
    ' Set WshNetwork = WScript.CreateObject("WScript.Network")
    ' CompName= WshNetwork.Computername ' Get Computername
    Exit For
  End If
Next

If StrIP = "" Then
  MsgBox("Can not get any enabled IP address!")
  Wscript.Quit
End If

Ini = LoadStringFromFile(INIFile)
If Ini = "" Then
  MsgBox("Can not load regify client INI file! [" & INIFile & "]")
  Wscript.Quit
End If

NewIni = Ini
If Left(StrIP, Len(LocalNet)) = LocalNet Then
  ' Intranet -> Set Proxy (old INI format)
  Mode = "intranet + proxy"
  objRegEx.Pattern = "ProxyServer =" & ValueExp
  NewIni = objRegEx.Replace(Ini, "ProxyServer = " & Proxy & vbCrLf)
  objRegEx.Pattern = "ProxyUsername =" & ValueExp
  NewIni = objRegEx.Replace(NewIni, "ProxyUsername = " & ProxyUser & vbCrLf)
  objRegEx.Pattern = "ProxyPassword =" & ValueExp
  NewIni = objRegEx.Replace(NewIni, "ProxyPassword = " & ProxyPass & vbCrLf)
  ' Intranet -> Set Proxy (new INI format)
  objRegEx.Pattern = "proxyServer =" & ValueExp
  NewIni = objRegEx.Replace(NewIni, "proxyServer = " & Proxy & vbCrLf)
  objRegEx.Pattern = "proxyUsername =" & ValueExp
  NewIni = objRegEx.Replace(NewIni, "proxyUsername = " & ProxyUser & vbCrLf)
  objRegEx.Pattern = "proxyPassword =" & ValueExp
  NewIni = objRegEx.Replace(NewIni, "proxyPassword = " & ProxyPass & vbCrLf)
Else
  ' Not Intranet, remove Proxy (old INI format)
  Mode = "internet, no proxy"
  objRegEx.Pattern = "ProxyServer =" & ValueExp
  NewIni = objRegEx.Replace(Ini, "ProxyServer =" & vbCrLf)
  objRegEx.Pattern = "ProxyUsername =" & ValueExp
  NewIni = objRegEx.Replace(NewIni, "ProxyUsername =" & vbCrLf)
  objRegEx.Pattern = "ProxyPassword =" & ValueExp
  NewIni = objRegEx.Replace(NewIni, "ProxyPassword =" & vbCrLf)
  ' Not Intranet, remove Proxy (new INI format)
  objRegEx.Pattern = "proxyServer =" & ValueExp
  NewIni = objRegEx.Replace(NewIni, "proxyServer =" & vbCrLf)
  objRegEx.Pattern = "proxyUsername =" & ValueExp
  NewIni = objRegEx.Replace(NewIni, "proxyUsername =" & vbCrLf)
  objRegEx.Pattern = "proxyPassword =" & ValueExp
  NewIni = objRegEx.Replace(NewIni, "proxyPassword = "& vbCrLf)
End If
If (NewIni <> Ini) Then
  ' Save changes
  SaveStringToFile INIFile, NewIni
  If Loud = True Then
    MsgBox("regify Client proxy settings have been changed (" & Mode & ")")
  End If
Else
  ' Nothing has changed
  If Loud = True Then
    MsgBox("regify Client proxy settings already ok (" & Mode & ")")
  End If
End If

' ---------- Helping functions ------------
Const fsoForReading = 1
Const fsoForWriting = 2
Function LoadStringFromFile(filename)
  Dim fso, f
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set f = fso.OpenTextFile(filename, fsoForReading)
  LoadStringFromFile = f.ReadAll
  f.Close
End Function

Sub SaveStringToFile(filename, text)
  Dim fso, f
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set f = fso.OpenTextFile(filename, fsoForWriting)
  f.Write text
  f.Close
End Sub