-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpacproxy.go
100 lines (85 loc) · 2.18 KB
/
pacproxy.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/williambailey/pacproxy/pac"
)
// Name of the app
const Name = "pacproxy"
// Version of the app
const Version = "2.0.6"
// About the app
const About = "A no-frills local HTTP proxy server powered by a proxy auto-config (PAC) file"
// Repo where the app is located
const Repo = "https://github.com/williambailey/pacproxy"
var (
fPac string
fListen string
fVerbose bool
)
func init() {
flag.StringVar(&fPac, "c", "", "PAC file name, url or javascript to use (required)")
flag.StringVar(&fListen, "l", "127.0.0.1:8080", "Interface and port to listen on")
flag.BoolVar(&fVerbose, "v", false, "send verbose output to STDERR")
}
func main() {
flag.Usage = func() {
// fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), "%s v%s\n\n%s\n%s\n\nUsage:\n", Name, Version, About, Repo)
flag.PrintDefaults()
}
flag.Parse()
seen := make(map[string]bool)
flag.Visit(func(f *flag.Flag) { seen[f.Name] = true })
required := []string{"c"}
for _, req := range required {
if !seen[req] {
exitWithUsage(fmt.Sprintf("Missing required flag -%s", req))
}
}
if strings.TrimSpace(fPac) == "" {
exitWithUsage("Unexpected empty value for -c")
}
if fVerbose {
log.SetOutput(os.Stderr)
} else {
log.SetOutput(ioutil.Discard)
}
log.SetPrefix("")
log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile | log.LUTC)
log.Printf("Starting %s v%s", Name, Version)
otto := pac.NewOttoEngine(
pac.OttoLoader(pac.SmartLoader(fPac)),
)
if err := otto.Start(); err != nil {
log.Panic(err)
}
defer otto.Stop()
initSignalNotify(otto)
srv := &http.Server{
Addr: fListen,
ReadHeaderTimeout: 2 * time.Second,
IdleTimeout: 60 * time.Second,
Handler: newProxyHTTPHandler(
otto,
&pac.FirstItemSelector{},
newNonProxyHTTPHandler(),
),
}
log.Printf("Listening on %q", fListen)
if err := srv.ListenAndServe(); err != nil {
log.Panic(err)
}
}
func exitWithUsage(message string) {
os.Stderr.WriteString(message)
os.Stderr.WriteString("\n")
flag.Usage()
os.Exit(2) // the same exit code flag.Parse uses
}