/
main.go
172 lines (137 loc) · 3.68 KB
/
main.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
/*
Command pianobarproxy is a simple SOCKS5 shim for pianobar.
This lets you proxy pianobar through ssh (or any SOCKS5 provider).
# Start your SOCKS5 proxy via ssh:
ssh -v -D localhost:9080 -C -N example.com
# Start pianobarproxy:
pianobarproxy -socks5 :9080
# Add the following to $HOME/.config/pianobar/config:
proxy = http://localhost:9090
Install
go get github.com/robertkrimen/pianobarproxy
(http://golang.org/doc/install)
Usage
Usage of pianobarproxy:
-listen="localhost:9090": The listening address
-socks5="localhost:1080": The address of the SOCKS5 proxy
*/
package main
import (
"code.google.com/p/go.net/proxy"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"strings"
)
var (
transport *http.Transport
dialer proxy.Dialer
)
var (
flag_listen = flag.String("listen", "localhost:9090", "The listening address")
flag_socks5 = flag.String("socks5", "localhost:1080", "The address of the SOCKS5 proxy")
)
func pipe(a io.ReadWriteCloser, b io.ReadWriteCloser) {
io.Copy(a, b)
a.Close()
b.Close()
}
func copyHeader(dst, src http.Header) {
for key, entry := range src {
for _, value := range entry {
dst.Add(key, value)
}
}
}
func httpProxy(writer http.ResponseWriter, request *http.Request) {
proxyRequest := new(http.Request)
*proxyRequest = *request
log.Printf("request = %s %s", request.Method, request.URL.Host)
if strings.ToUpper(proxyRequest.Method) == "CONNECT" {
hostPort := request.URL.Host
pandora, err := dialer.Dial("tcp", hostPort) // tuner.pandora.com:443
if err != nil {
log.Printf("pianobarproxy: error: %v", err)
writer.WriteHeader(http.StatusInternalServerError)
return
}
client, writer, err := writer.(http.Hijacker).Hijack()
writer.WriteString("HTTP/1.0 200 Connection Established\r\n\r\n")
writer.Flush()
go pipe(client, pandora)
go pipe(pandora, client)
return
}
proxyRequest.Proto = "HTTP/1.1"
proxyRequest.ProtoMajor = 1
proxyRequest.ProtoMinor = 1
proxyRequest.Close = false
// Remove the connection header to the backend. We want a
// persistent connection, regardless of what the client sent
// to us.
if proxyRequest.Header.Get("Connection") != "" {
proxyRequest.Header = make(http.Header)
copyHeader(proxyRequest.Header, request.Header)
proxyRequest.Header.Del("Connection")
}
response, err := transport.RoundTrip(proxyRequest)
if err != nil {
log.Printf("pianobarproxy: error: %v", err)
writer.WriteHeader(http.StatusInternalServerError)
return
}
copyHeader(writer.Header(), response.Header)
writer.WriteHeader(response.StatusCode)
if response.Body != nil {
io.Copy(io.Writer(writer), response.Body)
}
}
func getHost(target, defaultHost, defaultPort string) (string, error) {
host, port, err := net.SplitHostPort(target)
if err != nil {
return "", err
}
if host == "" {
host = defaultHost
}
if port == "" {
port = defaultPort
}
return host + ":" + port, nil
}
func main() {
flag.Parse()
socks5, err := getHost(*flag_socks5, "localhost", "1080")
if err != nil {
panic(err)
}
log.Printf("SOCKS5/--socks5 = %s", socks5)
{
var err error
dialer, err = proxy.SOCKS5("tcp", socks5, nil, &net.Dialer{})
if err != nil {
panic(err)
}
}
transport = &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr)
},
}
listen, err := getHost(*flag_listen, "localhost", "9090")
if err != nil {
panic(err)
}
log.Printf("listen/--listen = %s", listen)
{
fmt.Printf("%s\n\n %s = http://%s\n\n",
"# Add the following to $HOME/.config/pianobar/config:",
"proxy", listen,
)
}
log.Printf("pianobarproxy")
log.Fatal(http.ListenAndServe(listen, http.HandlerFunc(httpProxy)))
}