mirror of
https://github.com/zhigang1992/cow.git
synced 2026-01-12 22:46:29 +08:00
Optimize PAC generation.
This commit is contained in:
32
pac.go
32
pac.go
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@@ -9,8 +8,6 @@ import (
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var pacDirect = []byte("function FindProxyForURL(url, host) {return 'DIRECT'; };")
|
||||
|
||||
const pacRawTmpl = `var direct = 'DIRECT';
|
||||
var httpProxy = '{{.ProxyAddr}}';
|
||||
|
||||
@@ -90,6 +87,11 @@ func initProxyServerAddr() {
|
||||
}
|
||||
}
|
||||
|
||||
// No need for content-length as we are closing connection
|
||||
var pacHeader = []byte("HTTP/1.1 200 OK\r\nServer: cow-proxy\r\n" +
|
||||
"Content-Type: application/x-ns-proxy-autoconfig\r\nConnection: close\r\n\r\n")
|
||||
var pacDirect = []byte("function FindProxyForURL(url, host) { return 'DIRECT'; };")
|
||||
|
||||
func sendPAC(w io.Writer) {
|
||||
// domains in PAC file needs double quote
|
||||
ds1 := strings.Join(alwaysDirectDs.toArray(), "\",\n\"")
|
||||
@@ -103,9 +105,10 @@ func sendPAC(w io.Writer) {
|
||||
ds = ds1 + "\",\n\"" + ds2
|
||||
}
|
||||
if ds == "" {
|
||||
return pacDirect
|
||||
} else {
|
||||
ds = ",\n\"" + ds + "\""
|
||||
// Empty direct domain list
|
||||
w.Write(pacHeader)
|
||||
w.Write(pacDirect)
|
||||
return
|
||||
}
|
||||
|
||||
data := struct {
|
||||
@@ -113,19 +116,16 @@ func sendPAC(w io.Writer) {
|
||||
DirectDomains string
|
||||
}{
|
||||
proxyServerAddr,
|
||||
ds,
|
||||
",\n\"" + ds + "\"",
|
||||
}
|
||||
|
||||
if _, err := w.Write(pacHeader); err != nil {
|
||||
debug.Println("Error writing pac header")
|
||||
return
|
||||
}
|
||||
// debug.Println("direct:", data.DirectDomains)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
if err := pacTmpl.Execute(buf, data); err != nil {
|
||||
if err := pacTmpl.Execute(w, data); err != nil {
|
||||
errl.Println("Error generating pac file:", err)
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
header := new(bytes.Buffer)
|
||||
header.WriteString("HTTP/1.1 200 Okay\r\nServer: cow-proxy\r\nContent-Type: text/html\r\nConnection: close\r\n")
|
||||
header.WriteString(fmt.Sprintf("Content-Length: %d\r\n\r\n", buf.Len()))
|
||||
w.Write(header.Bytes())
|
||||
w.Write(buf.Bytes())
|
||||
}
|
||||
|
||||
2
proxy.go
2
proxy.go
@@ -725,7 +725,7 @@ func (h *Handler) setStateResponsReceived(host string) {
|
||||
}
|
||||
}
|
||||
|
||||
var connEstablished = []byte("HTTP/1.0 200 Connection established\r\nProxy-agent: cow-proxy/0.1\r\n\r\n")
|
||||
var connEstablished = []byte("HTTP/1.0 200 Connection established\r\nProxy-agent: cow-proxy\r\n\r\n")
|
||||
|
||||
// Do HTTP CONNECT
|
||||
func (h *Handler) doConnect(r *Request, c *clientConn) (err error) {
|
||||
|
||||
Reference in New Issue
Block a user