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

Fix PAC addr lost bug #535

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Modify configuration file `~/.cow/rc` (OS X or Linux) or `rc.txt` (Windows). A s

# Line starting with # is comment and will be ignored
# Local proxy listen address
# If cow is deployed behind Loadbalancer, you should specified PAC address(eg. foo.bar.com) like this
# listen = http://127.0.0.1:7777 foo.bar.com:7777
listen = http://127.0.0.1:7777

# SOCKS5 parent proxy
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ COW 的设计目标是自动化,理想情况下用户无需关心哪些网站
# 配置 HTTP 和 HTTPS 代理时请填入该地址
# 若配置代理时有对所有协议使用该代理的选项,且你不清楚此选项的含义,请勾选
# 或者在自动代理配置中填入 http://127.0.0.1:7777/pac
# 如果 cow 部署在负载均衡后面, 需要自定义 PAC 地址(例如: foo.bar.com)如下
# listen = http://127.0.0.1:7777 foo.bar.com:7777
listen = http://127.0.0.1:7777

# SOCKS5 二级代理
Expand Down
4 changes: 2 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var headRawTmpl = "HTTP/1.1 {{.CodeReason}}\r\n" +
"Connection: keep-alive\r\n" +
"Cache-Control: no-cache\r\n" +
"Pragma: no-cache\r\n" +
"Content-Type: text/html\r\n" +
"Content-Type: text/html;charset=utf-8\r\n" +
"Content-Length: {{.Length}}\r\n"

var errPageTmpl, headTmpl *template.Template
Expand All @@ -31,7 +31,7 @@ func init() {
<h1>{{.H1}}</h1>
{{.Msg}}
<hr />
Generated by <i>COW ` + version + `</i> <br />
<i>你电脑经过了代理, 如果是非预期记得关闭代理(系统, 终端等)设置</i> <br />
Host <i>` + hostName + `</i> <br />
{{.T}}
</body>
Expand Down
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/yujinqiu/cow

go 1.15

require (
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/cyfdecyf/bufio v0.0.0-20130801052708-9601756e2a6b
github.com/cyfdecyf/color v0.0.0-20130827105946-31d518c963d2
github.com/cyfdecyf/leakybuf v0.0.0-20140618011800-ffae040843be
github.com/shadowsocks/shadowsocks-go v0.0.0-20200409064450-3e585ff90601
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
)
16 changes: 9 additions & 7 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,14 @@ func (hp *httpProxy) Serve(wg *sync.WaitGroup, quit <-chan struct{}) {
}()
host, _, _ := net.SplitHostPort(hp.addr)
var pacURL string
if host == "" || host == "0.0.0.0" {
pacURL = fmt.Sprintf("http://<hostip>:%s/pac", hp.port)
} else if hp.addrInPAC == "" {
pacURL = fmt.Sprintf("http://%s/pac", hp.addr)
} else {
if hp.addrInPAC != "" {
pacURL = fmt.Sprintf("http://%s/pac", hp.addrInPAC)
} else {
if host == "" || host == "0.0.0.0" {
pacURL = fmt.Sprintf("http://<hostip>:%s/pac", hp.port)
} else {
pacURL = fmt.Sprintf("http://%s/pac", hp.addr)
}
}
info.Printf("COW %s listen http %s, PAC url %s\n", version, hp.addr, pacURL)

Expand Down Expand Up @@ -343,7 +345,7 @@ func isSelfRequest(r *Request) bool {
// But if client PAC setting is using cow server's DNS name, we can't
// decide if the request is for cow itself (need reverse lookup).
// So if request path seems like getting PAC, simply return true.
if r.URL.Path == "/pac" || strings.HasPrefix(r.URL.Path, "/pac?") {
if r.URL.Path == "/pac" || r.URL.Path == "/proxy.pac" || strings.HasPrefix(r.URL.Path, "/pac?") {
return true
}
r.URL.ParseHostPort(r.Header.Host)
Expand All @@ -361,7 +363,7 @@ func (c *clientConn) serveSelfURL(r *Request) (err error) {
if r.Method != "GET" {
goto end
}
if r.URL.Path == "/pac" || strings.HasPrefix(r.URL.Path, "/pac?") {
if r.URL.Path == "/pac" || r.URL.Path == "/proxy.pac" || strings.HasPrefix(r.URL.Path, "/pac?") {
sendPAC(c)
// PAC header contains connection close, send non nil error to close
// client connection.
Expand Down