forked from txthinking/brook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pac.go
180 lines (164 loc) · 3.9 KB
/
pac.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (c) 2016-present Cloud <cloud@txthinking.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package brook
import (
"bytes"
"context"
"io"
"net/http"
"os"
"text/template"
"time"
"strings"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/js"
)
type PAC struct {
Addr string
File string
Proxy string
DomainURL string
DomainData []byte
HTTPServer *http.Server
Body []byte
}
func NewPAC(addr, file, proxy, domainURL string) *PAC {
p := &PAC{
Addr: addr,
File: file,
Proxy: proxy,
DomainURL: domainURL,
}
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) {
var err error
l := make([]string, 0)
if p.DomainURL != "" {
l, err = ReadList(p.DomainURL)
if err != nil {
return nil, err
}
}
if p.DomainData != nil {
b := bytes.TrimSpace(p.DomainData)
b = bytes.Replace(b, []byte{0x20}, []byte{}, -1)
b = bytes.Replace(b, []byte{0x0d, 0x0a}, []byte{0x0a}, -1)
l = strings.Split(string(b), "\n")
}
t := template.New("pac")
t, err = t.Parse(tpl)
if err != nil {
return nil, err
}
b := &bytes.Buffer{}
if err := t.Execute(b, map[string]interface{}{
"proxy": p.Proxy,
"domains": l,
}); err != nil {
return nil, err
}
b1 := &bytes.Buffer{}
m := minify.New()
m.AddFunc("application/javascript", js.Minify)
if err := m.Minify("application/javascript", b1, b); err != nil {
return nil, err
}
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
}
p.Body, err = io.ReadAll(r)
if err != nil {
return err
}
return p.HTTPServer.ListenAndServe()
}
func (p *PAC) Shutdown() error {
return p.HTTPServer.Shutdown(context.Background())
}
func (p *PAC) WriteToFile() error {
r, err := p.MakeBody()
if err != nil {
return err
}
b, err := io.ReadAll(r)
if err != nil {
return err
}
if err := os.WriteFile(p.File, b, 0644); err != nil {
return err
}
return nil
}
func (p *PAC) WriteToStdout() error {
r, err := p.MakeBody()
if err != nil {
return err
}
if _, err := io.Copy(os.Stdout, r); err != nil {
return err
}
return nil
}
var tpl = `
var proxy="{{.proxy}}";
var domains = {
{{range .domains}}
"{{.}}": 1,
{{end}}
};
function ip4todecimal(ip) {
var d = ip.split('.');
return ((((((+d[0])*256)+(+d[1]))*256)+(+d[2]))*256)+(+d[3]);
}
function FindProxyForURL(url, host){
if(/\d+\.\d+\.\d+\.\d+/.test(host)){
if (isInNet(dnsResolve(host), "10.0.0.0", "255.0.0.0") ||
isInNet(dnsResolve(host), "172.16.0.0", "255.240.0.0") ||
isInNet(dnsResolve(host), "192.168.0.0", "255.255.0.0") ||
isInNet(dnsResolve(host), "127.0.0.0", "255.255.255.0")){
return "DIRECT";
}
return "DIRECT";
}
if (isPlainHostName(host)){
return "DIRECT";
}
var a = host.split(".");
for(var i=a.length-1; i>=0; i--){
if (domains.hasOwnProperty(a.slice(i).join("."))){
return "DIRECT";
}
}
return proxy;
}
`