Skip to content

Instantly share code, notes, and snippets.

@mjs
Created February 10, 2017 00:15
Show Gist options
  • Save mjs/f4efd4c11d64ff57015ac601973794c0 to your computer and use it in GitHub Desktop.
Save mjs/f4efd4c11d64ff57015ac601973794c0 to your computer and use it in GitHub Desktop.
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