-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathapiproxy.go
147 lines (136 loc) · 3.67 KB
/
apiproxy.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
package main
import (
"bytes"
"fmt"
"html/template"
"io"
"net/http"
"os"
"path"
"strings"
"time"
)
var DOMAIN_FORMAT_PREFIX = "/domain."
func main() {
http.HandleFunc("/", handleRequest)
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
host, port := os.Getenv("HOST"), os.Getenv("PORT")
var bind string
if host == "" {
bind = fmt.Sprintf(":%s", port)
} else {
bind = fmt.Sprintf("%s:%s", host, port)
}
err := http.ListenAndServe(bind, nil)
if err != nil {
panic(err)
}
}
func handleRequest(res http.ResponseWriter, req *http.Request) {
url, uri := req.URL, req.RequestURI
if url.Path == "/" {
handleWelcome(res, req)
} else if strings.HasPrefix(uri, DOMAIN_FORMAT_PREFIX) {
regulatedUri := uri[len(DOMAIN_FORMAT_PREFIX):]
slashIdx := strings.Index(regulatedUri, "/")
if slashIdx < 0 {
handleTwitterRequest(res, req, regulatedUri, "/")
} else {
handleTwitterRequest(res, req, regulatedUri[:slashIdx], regulatedUri[slashIdx:])
}
} else {
handleUnimplementedRequest(res, req)
}
}
func handleTwitterRequest(res http.ResponseWriter, req *http.Request, domain string, uri string) {
var twitterUri string
if domain == "" {
twitterUri = fmt.Sprintf("https://twitter.com%s", uri)
} else {
twitterUri = fmt.Sprintf("https://%s.twitter.com%s", domain, uri)
}
twitterReq, err := http.NewRequest(req.Method, twitterUri, req.Body)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
reqHeader := req.Header
copyHeader(reqHeader, twitterReq.Header)
reqHeader.Del("Cookie")
cookieDomain := fmt.Sprintf(".%s", req.Host)
for _, v := range req.Cookies() {
if strings.EqualFold(v.Domain, cookieDomain) {
v.Domain = ".twitter.com"
}
reqHeader.Add("Cookie", cookieToString(v))
}
twitterRes, err := http.DefaultTransport.RoundTrip(twitterReq)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
resHeader := res.Header()
copyHeader(twitterRes.Header, res.Header())
resHeader.Del("Set-Cookie")
for _, v := range twitterRes.Cookies() {
if strings.EqualFold(v.Domain, ".twitter.com") {
v.Domain = cookieDomain
}
resHeader.Add("Set-Cookie", cookieToString(v))
}
res.WriteHeader(twitterRes.StatusCode)
io.Copy(res, twitterRes.Body)
}
func handleUnimplementedRequest(res http.ResponseWriter, req *http.Request) {
errMessage := fmt.Sprintf("Unable to handle: %s, not implemented.", req.URL.Path)
http.Error(res, errMessage, http.StatusNotImplemented)
}
func handleWelcome(res http.ResponseWriter, req *http.Request) {
info := WelcomeInfo{"Twidere API Proxy", "0.9", req.Host}
fp := path.Join("templates", "welcome.html")
tmpl, err := template.ParseFiles(fp)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
if err := tmpl.Execute(res, info); err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
}
}
func copyHeader(in http.Header, out http.Header) {
for k, vs := range in {
for _, v := range vs {
out.Add(k, v)
}
}
}
func cookieToString(c *http.Cookie) string {
var b bytes.Buffer
fmt.Fprintf(&b, "%s=%s", c.Name, c.Value)
if len(c.Path) > 0 {
fmt.Fprintf(&b, "; Path=%s", c.Path)
}
if len(c.Domain) > 0 {
fmt.Fprintf(&b, "; Domain=%s", c.Domain)
}
if c.Expires.Unix() > 0 {
fmt.Fprintf(&b, "; Expires=%s", c.Expires.UTC().Format(time.RFC1123))
}
if c.MaxAge > 0 {
fmt.Fprintf(&b, "; Max-Age=%d", c.MaxAge)
} else if c.MaxAge < 0 {
fmt.Fprintf(&b, "; Max-Age=0")
}
if c.HttpOnly {
fmt.Fprintf(&b, "; HttpOnly")
}
if c.Secure {
fmt.Fprintf(&b, "; Secure")
}
return b.String()
}
type WelcomeInfo struct {
Name string
Version string
Host string
}