Created
February 10, 2017 00:15
-
-
Save mjs/f4efd4c11d64ff57015ac601973794c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"net/url" | |
"sync" | |
) | |
type ProxyConfig struct { | |
mu sync.Mutex | |
proxy *url.URL | |
} | |
func (ps *ProxyConfig) Set(proxyStr string) error { | |
ps.mu.Lock() | |
defer ps.mu.Unlock() | |
proxyURL, err := url.Parse(proxyStr) | |
if err != nil { | |
return err | |
} | |
ps.proxy = proxyURL | |
return nil | |
} | |
func (ps *ProxyConfig) GetProxy(req *http.Request) (*url.URL, error) { | |
ps.mu.Lock() | |
defer ps.mu.Unlock() | |
return ps.proxy, nil | |
} | |
func main() { | |
config := new(ProxyConfig) | |
//config.Set("http://1.2.3.4:8000/") | |
config.Set("http://127.0.0.1:8888/") | |
transport := http.DefaultTransport.(*http.Transport) | |
transport.Proxy = config.GetProxy | |
client := new(http.Client) | |
_, err := client.Get("http://google.com/") | |
fmt.Println(err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment