forked from rapid7/go-get-proxied
/
proxy.go
172 lines (155 loc) · 3.97 KB
/
proxy.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
// Copyright 2018, Rapid7, Inc.
// License: BSD-3-clause
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
package proxy
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
)
/*
Represents a Proxy which can be used to proxy communications.
*/
type Proxy interface {
// The Proxy's protocol
Protocol() string
// The Proxy's host (hostname or IP)
Host() string
// The Proxy's port
Port() uint16
// username, true: A username was specified
// username, false: A username was not specified, username should be considered "nil"
Username() (string, bool)
// password, true: A password was specified
// password, false: A password was not specified, password should be considered "nil"
Password() (string, bool)
// Human readable location where this Proxy was found
Src() string
// A fully qualified URL for this Proxy
URL() *url.URL
// A human readable representation of this Proxy. User info (if any) will be obfuscated. Use URL() if you need the URL with user info.
String() string
MarshalJSON() ([]byte, error)
toMap() map[string]interface{}
}
func NewProxy(u *url.URL, src string) (Proxy, error) {
proxy := new(proxy)
if err := proxy.init(u, src); err != nil {
return nil, err
}
return proxy, nil
}
const (
defaultPort = 8443
)
var defaultPorts = map[string]uint16{"": defaultPort, protocolHTTP: defaultPort, protocolHTTPS: defaultPort}
type proxy struct {
protocol string
host string
port uint16
user *url.Userinfo
src string
}
func (p *proxy) init(u *url.URL, src string) error {
if u == nil {
return errors.New("nil URL")
}
scheme := strings.TrimSpace(strings.ToLower(u.Scheme))
host, port, err := SplitHostPort(u)
if err != nil {
return err
}
if host == "" {
return errors.New("empty host")
}
if port == 0 {
port = defaultPorts[scheme]
}
if port == 0 {
port = defaultPorts[""]
}
if port == 0 {
return errors.New("port undefined")
}
p.protocol = scheme
p.host = host
p.port = port
p.user = u.User
p.src = src
return nil
}
func (p *proxy) Protocol() string {
return p.protocol
}
func (p *proxy) Host() string {
return p.host
}
func (p *proxy) Port() uint16 {
return p.port
}
func (p *proxy) Username() (string, bool) {
if p.user == nil {
return "", false
}
return p.user.Username(), true
}
func (p *proxy) Password() (string, bool) {
if p.user == nil {
return "", false
}
return p.user.Password()
}
func (p *proxy) Src() string {
return p.src
}
func (p *proxy) URL() *url.URL {
return &url.URL{
Scheme: p.Protocol(),
Host: fmt.Sprintf("%s:%d", p.Host(), p.Port()),
User: p.user,
}
}
func (p *proxy) String() string {
var auth string
if _, exists := p.Username(); exists {
if _, exists := p.Password(); exists {
auth = "<username>:<password>@"
} else {
auth = "<username>@"
}
}
return fmt.Sprintf("%s|%s://%s%s:%d", p.Src(), p.Protocol(), auth, p.Host(), p.Port())
}
func (p *proxy) MarshalJSON() ([]byte, error) {
return json.Marshal(p.toMap())
}
func (p *proxy) toMap() map[string]interface{} {
m := map[string]interface{}{
"protocol": p.Protocol(),
"host": p.Host(),
"port": p.Port(),
"src": p.Src(),
}
if usernameStr, exists := p.Username(); exists {
m["username"] = usernameStr
} else {
m["username"] = nil
}
if passwordStr, exists := p.Password(); exists {
m["password"] = passwordStr
} else {
m["password"] = nil
}
return m
}