Automatic proxy setting for regify client

From regify WIKI
Revision as of 08:38, 22 November 2017 by Regify (talk | contribs) (→‎Proxy pre-definition for new users)
(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.

PAC support

Important: Since version 4, the regify client supports PAC files and therefore is able to handle proxy switching by using such PAC file.

The VBScript down here is only applicable, if PAC files do not work for you.

Proxy pre-definition for new users

In some cases of software distribution you might need to pre-select or pre-define the proxy settings for new users. In such case, you can copy a pre-defined regify_client.ini file to the users application data folder. It does not matter if you do this before running the setup or after running the setup. It only should be done before the user is trying to run the regify client for the first time. Please do not overwrite existing regify_client.ini files.

To pre-select a proxy, please use a template like this for %APPDATA%\regify\regify_client.ini:

[USER]
regifyProviderName = dummy

[PROXY]
proxySelection = 1
proxyServer = proxy.company.com:3128
proxyPACFile = 
proxyUsername = 
proxyPassword = 

The possible values for proxySelection are: 0=no proxy, 1=use proxyServer, 2=use proxyPACFile

The dummy entry for regifyProviderName is important because otherwise the regify client considers the ini file as an older one that needs to get converted. In this case the proxy values will get lost.

You can also pre-define proxyUsername and proxyPassword. If you consider to use a proxy with NTLM authentication (Windows logon), please leave them with empty values.

Proxy selection using some script

Please first check, if some PAC file would fit your needs. The solution below is only suitable if PAC does not work for you.

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.
  • If you like to edit, we strongly suggest to use an editor that is able to edit vbs files (like PSPad or Notepad++ etc.)

The script

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

' Save as "setRegifyProxy.vbs" or similar

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