-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
78 lines (70 loc) · 1.48 KB
/
main.go
File metadata and controls
78 lines (70 loc) · 1.48 KB
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
package main
import (
"log"
"net/http"
"strings"
"github.com/elazarl/goproxy"
"regexp"
"os"
"bufio"
"io"
)
func main() {
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = false
proxy.OnRequest().HandleConnectFunc(
func(req string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
parts := strings.Split(ctx.Req.RemoteAddr, ":")
var ip string
if len(parts) > 0 {
ip = parts[0]
} else {
return goproxy.RejectConnect, req
}
if !IPInWhitelist(ip) {
return goproxy.RejectConnect, req
}
return goproxy.OkConnect, req
})
proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
parts := strings.Split(req.RemoteAddr, ":")
var ip string
if len(parts) > 0 {
ip = parts[0]
} else {
return req, nil
}
if !IPInWhitelist(ip) {
return req, goproxy.NewResponse(req,
goproxy.ContentTypeText, http.StatusForbidden, "")
}
return req, nil
})
log.Fatal(http.ListenAndServe(":8888", proxy))
}
var IPPool = []string{}
var regs []*regexp.Regexp
func init() {
fi,err := os.Open("/home/ubuntu/ip")
if err != nil{
panic("fail to initialize ip whitelist")
}
defer fi.Close()
br := bufio.NewReader(fi)
for {
ip, _, c := br.ReadLine()
if c == io.EOF {
break
}
regs = append(regs, regexp.MustCompile(string(ip)))
}
}
func IPInWhitelist(given string) bool {
for _, reg := range regs {
rs := reg.FindString(given)
if rs == given {
return true
}
}
return false
}