Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package to setup system wide HTTP(S) Proxy on Desktop Platforms #190

Merged
merged 76 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
e793764
add dynamic config support
amircybersec Jan 16, 2024
bbd9f82
Merge branch 'Jigsaw-Code:main' into main
amircybersec Jan 21, 2024
ba98871
Revert "add dynamic config support"
amircybersec Jan 21, 2024
bcb602e
Merge branch 'Jigsaw-Code:main' into main
amircybersec Feb 14, 2024
4cb44eb
Merge branch 'Jigsaw-Code:main' into main
amircybersec Feb 24, 2024
c11ab04
add sys proxy for desktop platforms
amircybersec Feb 26, 2024
fdf398f
do nothing on unsupported platforms
amircybersec Feb 26, 2024
6a5680c
add proxyType and clean up code for Darwin
amircybersec Feb 29, 2024
f48a095
remove safeclose and clean up the package doc
amircybersec Feb 29, 2024
cc694c9
moved package docs to doc.go
amircybersec Feb 29, 2024
044d794
return an error if platform is not supported
amircybersec Feb 29, 2024
422ca10
refactored runCommad
amircybersec Feb 29, 2024
e2247d5
added docs and clean up
amircybersec Mar 1, 2024
f91cafc
linux code refactor and cleanup
amircybersec Mar 1, 2024
65db63e
Update x/sysproxy/doc.go
amircybersec Mar 6, 2024
f48f419
Update x/sysproxy/sysproxy_darwin.go
amircybersec Mar 6, 2024
ed4c93b
Update x/sysproxy/sysproxy_darwin.go
amircybersec Mar 6, 2024
ceec53f
Update x/sysproxy/sysproxy_windows.go
amircybersec Mar 6, 2024
5952a45
Update x/sysproxy/sysproxy_windows.go
amircybersec Mar 6, 2024
0faac88
Update x/sysproxy/sysproxy_windows.go
amircybersec Mar 6, 2024
7339b23
Update x/sysproxy/sysproxy_windows.go
amircybersec Mar 6, 2024
7b895a5
Update x/sysproxy/sysproxy_windows.go
amircybersec Mar 6, 2024
b6f3c97
Update x/sysproxy/sysproxy_darwin.go
amircybersec Mar 6, 2024
6c59ad8
Update x/sysproxy/sysproxy_windows.go
amircybersec Mar 6, 2024
a04317e
Update x/sysproxy/sysproxy_windows.go
amircybersec Mar 6, 2024
7dd57d0
Update x/sysproxy/sysproxy_linux.go
amircybersec Mar 6, 2024
75ea37c
windows: backup settings before change & revert
amircybersec Mar 16, 2024
bdc6d2d
linux: add backup settings + socks5
amircybersec Mar 16, 2024
f8906a1
darwin: backup settings + socks
amircybersec Mar 16, 2024
2826419
return error if socks5 is not supported
amircybersec Mar 16, 2024
d3020d3
updated readme.md
amircybersec Mar 17, 2024
34ce3c3
updated doc.go
amircybersec Mar 17, 2024
0522adb
removed backup settings from Darwin
amircybersec Mar 24, 2024
7cf4822
windows: add socks, remove backup
amircybersec Mar 24, 2024
1ef992a
linux: replaced backup with clearing
amircybersec Mar 24, 2024
029a9c4
windows: removed backup method
amircybersec Mar 24, 2024
3f736c9
windows: bug fix
amircybersec Mar 24, 2024
1baff92
darwin: added back getProxySetting for testing
amircybersec Mar 24, 2024
3c593f9
darwin: added back getHostandPort
amircybersec Mar 24, 2024
9d8c9c9
added back ProxySettings type
amircybersec Mar 24, 2024
b6558e2
add getWebProxy and getSocksProxy
amircybersec Mar 25, 2024
2594efb
linux: add getwebproxy and getsocksproxy
amircybersec Mar 25, 2024
984833e
darwin: added tests
amircybersec Mar 25, 2024
66658ac
windows: add getproxysetting
amircybersec Mar 25, 2024
1dedbf7
one test file for all platforms
amircybersec Mar 25, 2024
630fc56
updating tests
amircybersec Mar 25, 2024
4765b77
fixed getsocksproxy issue on windows
amircybersec Mar 25, 2024
3f15492
linux: getsocksproxy bug fix
amircybersec Mar 25, 2024
4700f68
test clean up
amircybersec Mar 25, 2024
8855f10
windows: made proxySettings private
amircybersec Mar 25, 2024
8d5f311
remove print line
amircybersec Mar 25, 2024
c7e805c
linux: removed proxysettings struct
amircybersec Mar 25, 2024
4405b78
darwin: proxySettings made private
amircybersec Mar 25, 2024
38e2535
update docs
amircybersec Mar 25, 2024
9cfad5f
update readme
amircybersec Mar 25, 2024
4fd18a6
typo fix
amircybersec Mar 25, 2024
428f291
Update x/sysproxy/README.md
amircybersec Mar 25, 2024
7490cd1
Update x/sysproxy/README.md
amircybersec Mar 25, 2024
00f414c
update go.mod file + delete readme
amircybersec Mar 26, 2024
17aeeb7
update go.doc file
amircybersec Mar 26, 2024
b3c6f61
doc polish
amircybersec Mar 26, 2024
e49e0da
Merge branch 'main' into sysproxy-amir
amircybersec Mar 26, 2024
136c2db
changed clear proxy to disable proxy
amircybersec Mar 27, 2024
3fbb4ed
test: check if proxy setting is cleared
amircybersec Mar 27, 2024
e5b3ad1
disable proxy instead of clearing it
amircybersec Mar 28, 2024
7d22038
linux: diable proxy instead of clearing it
amircybersec Mar 28, 2024
235ad1d
linux: get proxy mode bugfix
amircybersec Mar 28, 2024
7e4abd9
linux: fixed enabled comparison bug
amircybersec Mar 28, 2024
d9c10c8
Update x/sysproxy/doc.go
amircybersec Apr 1, 2024
3da2ff2
Update x/sysproxy/sysproxy_test.go
amircybersec Apr 1, 2024
05e3d14
Update x/sysproxy/sysproxy_windows.go
amircybersec Apr 1, 2024
d71b726
Update x/sysproxy/sysproxy_windows.go
amircybersec Apr 1, 2024
7cdec08
doc update
amircybersec Apr 1, 2024
77faf78
change unset to diable in other platforms
amircybersec Apr 1, 2024
b34db8e
exclude ios explictly
amircybersec Apr 1, 2024
35273de
return joined errors
amircybersec Apr 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions x/sysproxy/README.md
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this to the Go doc instead. No one will read the README.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed README and merged content with Godoc

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Platform support

The table below summarizes system-wide support this package offers for various types of proxies on desktop platforms.

| Proxy Type | Windows | MacOS | Linux |
| --- | ----------- | ------ | ------ |
| HTTP | Yes | Yes | Yes
| HTTPS | Yes | Yes | Yes
| SOCKS | No | Yes | Yes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you do support Windows, but it's SOCKS4?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this socks server package that implements sock4, 4a, and 5:

https://github.com/cybozu-go/usocksd?tab=readme-ov-file

I wanted to test socks proxy on all platforms and see what versions they support in reality. I have tested linux and mac with socks 5 and they work fine. I could not get windows working with socks5 and it only worked with sock4 server in my tests. I have not tested mac and linux against socks4 though...Maybe we can do this later as TODO item.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated doc.go to reflect this in the documentation language



`SetupWebProxy` implementation in this package setups both HTTP and HTTPS proxy when they distinguished by the platform. `SetupSOCKSProxy` sets up SOCKS proxy that proxies all TCP-based connections.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to explain what setting a HTTP proxy and setting a HTTPS proxy mean.


For example, on MacOS, if you have HTTP Proxy setup and visit a website over HTTPS, your traffic does NOT go through the proxy.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rearrange the notes. Perhaps in a "Platform considerations" section. Then group each OS and pull the "other notes".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fortuna I rewrote the godoc and rearranged the notes.


However, if only HTTPS proxy is setup, both HTTP and HTTPS requests go through the HTTPS proxy.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For example, on MacOS, if you have HTTP Proxy setup and visit a website over HTTPS, your traffic does NOT go through the proxy.
However, if only HTTPS proxy is setup, both HTTP and HTTPS requests go through the HTTPS proxy.
On MacOS, if you have HTTP Proxy setup and visit a website over HTTPS, your traffic does NOT go through the proxy. However, if only HTTPS proxy is setup, both HTTP and HTTPS requests go through the HTTPS proxy. This library sets both proxies.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you set HTTPS, HTTP will use it? That's very counter intuitive.
Does it work the same in the other platforms? It would help to characterize that.

Copy link
Contributor Author

@amircybersec amircybersec Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fortuna The proxy server running locally is HTTP proxy server in both cases.

In the first experiment, I only setup system's client HTTP proxy settings. In this case, only HTTP requests get routed through the proxy. If I setup system's client HTTPS proxy settings, both HTTP and HTTPS requests get routed through the proxy.

I believe this is just the way the system decides which type of request to route to the proxy. HTTPS is basically the super set in this design.

In our case, we don't care much about this since we are settings up both at the same time on all platforms and call it WebProxy which ends up routing both HTTP and HTTPS requests through the proxy.


If SOCKS is setup, both HTTP and HTTPS requests go through the proxy since it is performed at the TCP layer. SOCKS does not support UDP.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SOCKS5 does support UDP. Please rephrase.

Did you mean the OS implementation doesn't support it? Did you test on all platforms?
I guess we can say that of SOCKS4 on windows, but SOCKS5 should support it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Sock5 does support UDP. I will update the language


On Windows, The client only supports SOCKS4 spec and cannot connecto SOCKS5 proxy.
amircybersec marked this conversation as resolved.
Show resolved Hide resolved

Support for FTP Proxy setting was not included due lack of adoption and usage. Username and password authentication was not included due to potential unreliability and untestd behavior. Please note that in SOCKS and HTTP proxy, credentials are communicated in plain text.
amircybersec marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say that authentication is not supported because the intended usage it to connect to a proxy running on localhost.


If you have a need for any of those, feel free to open an issue and let me know about the use case.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can delete this.

Suggested change
If you have a need for any of those, feel free to open an issue and let me know about the use case.


## Other notes

1. Windows does not explicitly distinguish between HTTP and HTTPS proxy. Also, username/password support authentication is not supported.

2. MacOS SOCKS client does not seems to correctly support authentication even thoguh it accepts credentials [[ref](https://discussions.apple.com/thread/255394737?sortBy=best)].

3. On GNOME, username/Pass authenticartion is not currently supported for HTTPS proxy [[ref](https://gitlab.gnome.org/GNOME/gsettings-desktop-schemas/-/issues/42)].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
3. On GNOME, username/Pass authenticartion is not currently supported for HTTPS proxy [[ref](https://gitlab.gnome.org/GNOME/gsettings-desktop-schemas/-/issues/42)].
3. On GNOME, username/Pass authentication is not currently supported for HTTPS proxy [[ref](https://gitlab.gnome.org/GNOME/gsettings-desktop-schemas/-/issues/42)].

80 changes: 80 additions & 0 deletions x/sysproxy/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2024 Jigsaw Operations LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*
jyyi1 marked this conversation as resolved.
Show resolved Hide resolved
Package sysproxy provides a simple interface to set/unset system-wide proxy settings.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cleaner:

Suggested change
/*
Package sysproxy provides a simple interface to set/unset system-wide proxy settings.
/*
Package sysproxy provides a simple interface to set or clear system-wide proxy settings.


# Platform Support

Currently this package supports desktop platforms only. The following platforms are supported:
amircybersec marked this conversation as resolved.
Show resolved Hide resolved
- macOS
amircybersec marked this conversation as resolved.
Show resolved Hide resolved
- Linux (Gnome)
- Windows

# macOS

To configure proxy settings on macOS, we use networksetup utility with following options. To set web proxy:

networksetup -setwebproxy <networkservice> <domain> <portnumber>

To set secure web proxy:

networksetup -setsecurewebproxy <networkservice> <domain> <portnumber>

For more information, see the link [here].

# Linux

Currently only GNOME is supported. This package uses gsettings untility to setup proxy settings. The following commands are used to set proxy settings:

gsetting set org.gnome.system.proxy.http host 'proxy.example.com'
gsetting set org.gnome.system.proxy.http port 8080

The following parameters can be set for other types of proxies:

org.gnome.system.proxy.https
org.gnome.system.proxy.ftp
org.gnome.system.proxy.socks

For more information, you can checkout the documentation for [gsettings] and its [configuration].

# Windows

On Windows, the package uses HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings + InternetSetOptionW
to setup proxy settings. For more information, you can checkout the documentation for [InternetSetOptionW].

# Usage

To set up system-wide proxy settings, use the [SetWebProxy] or [SetSOCKSProxy] methods to connect to a Web (HTTP & HTTPS) or SOCKS proxy.
This function takes two arguments: the IP address / hostname and the port of the proxy server.

To clear system-wide proxy settings, use the [ClearWebProxy] or [ClearSOCKSProxy] function.
This will set the address and port to "127.0.0.1:0" and disable the proxy.

To ensure that the system-wide proxy settings are unset upon program termination, it is recommended to call:

defer ClearWebProxy()

// or

defer ClearSOCKSProxy()

after the setting the proxy.

[here]: https://keith.github.io/xcode-man-pages/networksetup.8.html
[gsettings]: https://github.com/GNOME/gsettings-desktop-schemas/blob/master/schemas/org.gnome.system.proxy.gschema.xml.in
[configuration]: https://developer-old.gnome.org/ProxyConfiguration/
[InternetSetOptionW]: https://learn.microsoft.com/en-us/windows/win32/api/wininet/nf-wininet-internetsetoptionw
*/
package sysproxy
Loading
Loading