-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
63 lines (53 loc) · 1.92 KB
/
server.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
package main
// Copyright (c) 2013-2020, Sapphire Cat <https://github.com/sapphirecat>. All
// rights reserved. See the accompanying LICENSE file for license terms.
import (
"log"
"net/http"
"gopkg.in/elazarl/goproxy.v1"
)
// UseRulesetForConnect returns the goproxy function to handle TLS-secured HTTPS.
func UseRulesetForConnect(ruleset Ruleset) func(string, *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
return func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
target := getTarget(ruleset, host, RuleForTLS)
if target == NoOp {
target = host
if ruleset.Verbose {
log.Println("!match HTTPS", host)
}
} else if ruleset.Verbose {
log.Println("+HTTPS", host, ctx.Req.URL.Path)
}
return goproxy.OkConnect, target
}
}
// UseRulesetForHTTP returns the goproxy function to handle plain-text HTTP.
func UseRulesetForHTTP(ruleset Ruleset) func(*http.Request, *goproxy.ProxyCtx) (*http.Request, *http.Response) {
return func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
host := r.URL.Host
target := getTarget(ruleset, host, RuleForHTTP)
if target != NoOp {
r.URL.Host = target
if ruleset.Verbose {
log.Println("+plain", host, r.URL.Path)
}
} else if ruleset.Verbose {
log.Println("!match plain", r.URL.Host)
}
return r, nil
}
}
// UseRuleset configures devproxy to process the rules on goproxy requests.
func UseRuleset(proxy *goproxy.ProxyHttpServer, rules Ruleset) {
proxy.OnRequest().HandleConnectFunc(UseRulesetForConnect(rules))
proxy.OnRequest().DoFunc(UseRulesetForHTTP(rules))
}
// NewServer creates a goproxy that is configured to use the Ruleset.
func NewServer(r Ruleset, debugProxy bool) http.Handler {
// Create the proxy and set debugging on it
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = debugProxy
// Set up rules *in goproxy* that apply the ruleset to each request.
UseRuleset(proxy, r)
return proxy
}