Changing Firefox proxy settings?
How ca开发者_运维技巧n I change Firefox's proxy settings from a script like a vb script or maybe .net? If someone could show me how to do this it would be great.
You can do it by writing to the prefs.js
file, however, my understanding is that this file is only loaded on startup, and Firefox might overwrite your changes when shutting down so I think the only safe way of doing this would be to:
- Shut down all Firefox processes.
- Change
prefs.js
. - Start up Firefox.
This Code project article should show you how to close down a process from vbscript: DestroyWindow in VBScript
You can use the FileSystemObject to read/write to files.
I think something like this would work for starting it up again:
Set shell = CreateObject("WScript.Shell")
shell.Run Chr(34) & "PathToFireFox.exe" & Chr(34), 1, false
/*This program will find the path to application data of user,and get the path
(\Mozilla\Firefox\Profiles\*.default) and find proxy from the file prefs.js */
#include <iostream>
#include <sstream>
#include <fstream>
#include <windows.h>
#include <string>
#include <shlobj.h>
#include <winerror.h>
#include <vector>
#define MAX 8192
using namespace std;
wstring ReadProxyFromFile(wstring fileName);
wstring ParseAndGetProxyDetails (wstring strLine, wstring field);
wstring ReadINIFileAndGetFirefoxProfilePath( wstring &profileFilePath );
///
/// This function converts wstring to string.
///
string ToString(wchar_t const * pwch, UINT code_page)
{
// Allocate the appropriate space
int sizeRequired = WideCharToMultiByte( code_page, 0, pwch, -1, NULL, 0, NULL, NULL);
char * ach = new char[sizeRequired + 1];
// Convert it
WideCharToMultiByte( code_page, 0, pwch, -1, ach, sizeRequired, NULL, NULL);
// Put it into the return value
string ret(ach);
// Clean up
delete[] ach;
// Return the new string
return ret;
}
///
/// This function splits a string into fields using a separator.
///
vector<string> Split(string const & str_, char const * sep)
{
string str = str_.c_str();
size_t sep_len = ::strlen(sep);
vector<string> ret;
for (size_t i = str.find(sep); i != str.npos; i = str.find(sep))
{
ret.push_back(str.substr(0, i));
str = str.substr(i + sep_len, str.npos);
}
if (str.length() > 0)
ret.push_back(str);
return ret;
}
///
/// This function converts string to wstring.
///
wstring ToWStr(string const& x, UINT code_page )
{
// Allocate the appropriate space
int sizeRequired = MultiByteToWideChar(code_page, 0, x.c_str(), -1, NULL, 0);
wchar_t * ach = new wchar_t[sizeRequired + 1];
// Convert it
MultiByteToWideChar(code_page, 0, x.c_str(), -1, ach, sizeRequired);
// Put it into the return value
wstring ret(ach);
// Clean up
delete[] ach;
// Return the new string
return ret;
}
int main()
{
LPWSTR wszPath = NULL;
HRESULT hr;
wstring strPath;
hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData , KF_FLAG_CREATE, NULL, &wszPath);
if (SUCCEEDED(hr))
{
strPath = wszPath;
strPath += L"\\Mozilla\\Firefox";
wstring profileFilePath = strPath + L"\\profiles.ini";
wstring strCurrentProfile = ReadINIFileAndGetFirefoxProfilePath(profileFilePath);
wstring prefsPath = strPath + L"\\" + strCurrentProfile + L"\\prefs.js";
wstring strProxy = ReadProxyFromFile(prefsPath);
if (strProxy.length()>0)
{
wcout << "Proxy is " <<strProxy <<endl;
}
return 0;
}
}
wstring ReadINIFileAndGetFirefoxProfilePath( wstring &profileFilePath )
{
// reading each section from ini file
LPWSTR pSecNames= new WCHAR[1024];
int nSectionNum = 1;
wstring strAllSectionNames;
DWORD retVal = GetPrivateProfileSectionNames(pSecNames,1024,profileFilePath.c_str());
if (retVal)
{
for (unsigned int i =0; i < retVal; i++)
{
if (pSecNames[i] == NULL)
{
strAllSectionNames += L"_";
}
else
{
strAllSectionNames += pSecNames[i];
}
}
delete pSecNames;
string sectionNames = ToString(strAllSectionNames.c_str(),CP_UTF8);
vector <string> sectionNamesVector = Split(sectionNames.c_str(),"_");
wstring strSectionName;
for ( int it = 0 ; it < sectionNamesVector.size(); it++ )
{
strSectionName = ToWStr(sectionNamesVector.at(it),CP_UTF8);
wcout << nSectionNum << ". Section name: " << strSectionName;
LPWSTR pProfile= new WCHAR[1024];
DWORD active = GetPrivateProfileString(strSectionName.c_str(),L"Default",NULL,pProfile,MAX,profileFilePath.c_str());
if(active)
{
GetPrivateProfileString(strSectionName.c_str(),L"Path",NULL,(LPWSTR)pProfile,MAX,profileFilePath.c_str());
wstring strPath = pProfile;
delete pProfile;
return strPath;
}
delete pProfile;
nSectionNum++;
}
}
}
wstring ReadProxyFromFile(wstring fileName)
{
wstring proxyType;
wstring proxyHttp;
wstring httpPort;
wstring noProxiesOn;
wstring autoconfigUrl;
wstring proxySettings;
wstring field;
int nProxyType;
wifstream myfile;
wstring strLine;
size_t pos;
myfile.open (fileName.c_str());
if (myfile.is_open())
{
while (!myfile.eof())
{
getline(myfile,strLine);
if (!strLine.find(L"user_pref"))
{
pos = strLine.find_first_of(L", ");
field = strLine.substr(11,pos-12);
if (field == L"network.proxy.type")
{
proxyType = ParseAndGetProxyDetails(strLine, field);
nProxyType = _wtoi(proxyType.c_str());
}
if (field == L"network.proxy.http")
{
proxyHttp = ParseAndGetProxyDetails(strLine, field);
}
if (field == L"network.proxy.http_port")
{
httpPort = ParseAndGetProxyDetails(strLine, field);
}
if (field == L"network.proxy.autoconfig_url")
{
autoconfigUrl = ParseAndGetProxyDetails(strLine, field);
}
if (field == L"network.proxy.no_proxies_on")
{
noProxiesOn = ParseAndGetProxyDetails(strLine, field);
}
}
}
}
myfile.close();
switch (nProxyType)
{
case 0: // no proxy
proxySettings = L"FF:";
break;
case 1: // manual proxy
proxySettings = L"FF:PXY:"+proxyHttp+L":"+httpPort;
break;
case 2: //autoconfig_url
proxySettings = L"FF:URL:"+autoconfigUrl;
break;
case 4: // auto proxy
proxySettings = L"FF:";
break;
default: // system proxy
proxySettings = L"FF::";
break;
}
return proxySettings;
}
wstring ParseAndGetProxyDetails (wstring strLine, wstring field)
{
size_t position;
wstring proxyDetail;
position = strLine.find(field);
if (position != string::npos)
{
proxyDetail = strLine.substr(position);
position = proxyDetail.find_first_of(L",");
proxyDetail = proxyDetail.substr(position+1);
position = proxyDetail.find_first_of(L");");
proxyDetail = proxyDetail.substr(0,position);
}
return proxyDetail;
}
精彩评论