Skip to content

Commit

Permalink
pac
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Apr 29, 2020
1 parent bf29a10 commit 19c1cc9
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions pac.go
Expand Up @@ -27,17 +27,29 @@ type PAC struct {
DomainData []byte
CidrData []byte
HTTPServer *http.Server
Body []byte
}

func NewPAC(addr, file, proxy, mode, domainURL, cidrURL string) *PAC {
return &PAC{
p := &PAC{
Addr: addr,
File: file,
Proxy: proxy,
Mode: mode,
DomainURL: domainURL,
CidrURL: cidrURL,
}
mux := http.NewServeMux()
mux.Handle("/", p)
p.HTTPServer = &http.Server{
Addr: p.Addr,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
MaxHeaderBytes: 1 << 20,
Handler: mux,
}
return p
}

func (p *PAC) MakeBody() (io.Reader, error) {
Expand Down Expand Up @@ -110,26 +122,20 @@ func (p *PAC) MakeBody() (io.Reader, error) {
return b1, nil
}

func (p *PAC) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/x-ns-proxy-autoconfig")
w.Write(p.Body)
}

func (p *PAC) ListenAndServe() error {
r, err := p.MakeBody()
if err != nil {
return err
}
b, err := ioutil.ReadAll(r)
p.Body, err = ioutil.ReadAll(r)
if err != nil {
return err
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/x-ns-proxy-autoconfig")
w.Write(b)
})
p.HTTPServer = &http.Server{
Addr: p.Addr,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
MaxHeaderBytes: 1 << 20,
}
return p.HTTPServer.ListenAndServe()
}

Expand Down

0 comments on commit 19c1cc9

Please sign in to comment.