-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathiproxy.go
More file actions
142 lines (123 loc) · 2.88 KB
/
Copy pathiproxy.go
File metadata and controls
142 lines (123 loc) · 2.88 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
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
package main
import (
"flag"
"fmt"
"github.com/armon/go-socks5"
"github.com/elazarl/goproxy"
"io"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
type Props struct {
addr string
bind string
socks int
http int
discovery int
verbose bool
location bool
}
func main() {
props := Props{
addr: *flag.String("a", "172.20.10.1", "Proxy address to expose to clients"),
bind: *flag.String("b", "0.0.0.0", "Address to bind to"),
socks: *flag.Int("s", 0, "SOCKS5 proxy port"),
http: *flag.Int("p", 0, "HTTP proxy port"),
discovery: *flag.Int("d", 0, "HTTP port for auto proxy configuration discovery"),
location: *flag.Bool("l", false, "Whether to pool location details"),
verbose: *flag.Bool("v", false, "Enable verbose output"),
}
help := *flag.Bool("h", false, "Show help")
if help {
flag.PrintDefaults()
os.Exit(0)
}
if props.discovery != 0 {
go httpAutoDiscover(props)
}
if props.http != 0 {
go httpProxy(props)
}
if props.socks != 0 {
go socksProxy(props)
}
if props.location {
go fetchLocation(props.verbose)
}
loop()
}
func fetchLocation(verbose bool) {
fmt.Println("Starting location streaming")
reader, err := os.Open("/dev/location")
if err != nil {
fmt.Println("Failed reading location data")
}
p := make([]byte, 256)
for {
n, err := reader.Read(p)
if err == io.EOF {
fmt.Println("Reached end of location data")
}
if verbose {
fmt.Printf("%q", p[:n])
}
}
}
func httpAutoDiscover(props Props) {
addr := fmt.Sprint(props.bind, ":", props.discovery)
fmt.Println("Starting http discovery at", addr)
handler := func(w http.ResponseWriter, _ *http.Request) {
if props.verbose {
fmt.Println("Serving proxy discovery request")
}
funct := "function FindProxyForURL (url, host) {\n return '"
if props.socks != 0 {
funct += fmt.Sprint("SOCKS5 ", props.addr, ":", props.socks, "; ")
}
if props.http != 0 {
funct += fmt.Sprint("HTTP ", props.addr, ":", props.http, "; ")
}
funct += "DIRECT';}"
_, err := io.WriteString(w, funct)
if err != nil {
return
}
}
http.HandleFunc("/proxy", handler)
log.Fatal(http.ListenAndServe(addr, nil))
}
func loop() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
os.Exit(1)
}()
for {
time.Sleep(1 * time.Second)
}
}
func httpProxy(props Props) {
addr := fmt.Sprint(props.addr, ":", props.http)
fmt.Println("Starting http proxy at", addr)
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = props.verbose
log.Fatal(http.ListenAndServe(addr, proxy))
}
func socksProxy(props Props) {
addr := fmt.Sprint(props.addr, ":", props.socks)
fmt.Println("Starting socks5 proxy at", addr)
conf := &socks5.Config{}
server, err := socks5.New(conf)
if err != nil {
panic(err)
}
// Create SOCKS5 proxy on localhost port 8000
if err := server.ListenAndServe("tcp", addr); err != nil {
panic(err)
}
}