FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sourcegraph/cmd/github-proxy/github-proxy.go at master · IntelDan/sourcegraph · GitHub
IntelDan
/
sourcegraph
Public
forked from
sourcegraph/sourcegraph-public-snapshot
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sourcegraph
/
cmd
/
github-proxy
/
github-proxy.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
169 lines (148 loc) · 4.2 KB
Breadcrumbs
sourcegraph
/
cmd
/
github-proxy
/
github-proxy.go
Copy path
File metadata and controls
169 lines (148 loc) · 4.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
main
import
(
"bytes"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"github.com/gorilla/handlers"
"github.com/prometheus/client_golang/prometheus"
log15
"gopkg.in/inconshreveable/log15.v2"
"github.com/sourcegraph/sourcegraph/pkg/conf"
"github.com/sourcegraph/sourcegraph/pkg/debugserver"
"github.com/sourcegraph/sourcegraph/pkg/env"
"github.com/sourcegraph/sourcegraph/pkg/tracer"
)
var
(
logRequests
,
_
=
strconv
.
ParseBool
(
env
.
Get
(
"LOG_REQUESTS"
,
""
,
"log HTTP requests"
))
)
const
port
=
"3180"
// requestMu ensures we only do one request at a time to prevent tripping abuse detection.
var
requestMu
sync.
Mutex
var
rateLimitRemainingGauge
=
prometheus
.
NewGaugeVec
(prometheus.
GaugeOpts
{
Namespace
:
"src"
,
Subsystem
:
"github"
,
Name
:
"rate_limit_remaining"
,
Help
:
"Number of calls to GitHub's API remaining before hitting the rate limit."
,
}, []
string
{
"resource"
})
func
init
() {
rateLimitRemainingGauge
.
WithLabelValues
(
"core"
).
Set
(
5000
)
rateLimitRemainingGauge
.
WithLabelValues
(
"search"
).
Set
(
30
)
prometheus
.
MustRegister
(
rateLimitRemainingGauge
)
}
// list obtained from httputil of headers not to forward.
var
hopHeaders
=
map
[
string
]
struct
{}{
"Connection"
: {},
"Proxy-Connection"
: {},
// non-standard but still sent by libcurl and rejected by e.g. google
"Keep-Alive"
: {},
"Proxy-Authenticate"
: {},
"Proxy-Authorization"
: {},
"Te"
: {},
// canonicalized version of "TE"
"Trailer"
: {},
// not Trailers per URL above; http://www.rfc-editor.org/errata_search.php?eid=4522
"Transfer-Encoding"
: {},
"Upgrade"
: {},
}
func
main
() {
env
.
Lock
()
env
.
HandleHelpFlag
()
tracer
.
Init
()
go
func
() {
c
:=
make
(
chan
os.
Signal
,
1
)
signal
.
Notify
(
c
,
syscall
.
SIGINT
,
syscall
.
SIGHUP
)
<-
c
os
.
Exit
(
0
)
}()
go
debugserver
.
Start
()
var
(
authenticateRequestMu
sync.
RWMutex
authenticateRequest
func
(
query
url.
Values
,
header
http.
Header
)
)
conf
.
Watch
(
func
() {
cfg
:=
conf
.
Get
()
if
clientID
,
clientSecret
:=
cfg
.
GithubClientID
,
cfg
.
GithubClientSecret
;
clientID
!=
""
&&
clientSecret
!=
""
{
authenticateRequestMu
.
Lock
()
authenticateRequest
=
func
(
query
url.
Values
,
header
http.
Header
) {
query
.
Set
(
"client_id"
,
clientID
)
query
.
Set
(
"client_secret"
,
clientSecret
)
}
authenticateRequestMu
.
Unlock
()
}
})
var
h
http.
Handler
=
http
.
HandlerFunc
(
func
(
w
http.
ResponseWriter
,
r
*
http.
Request
) {
q2
:=
r
.
URL
.
Query
()
h2
:=
make
(http.
Header
)
for
k
,
v
:=
range
r
.
Header
{
if
_
,
found
:=
hopHeaders
[
k
];
!
found
{
h2
[
k
]
=
v
}
}
// Authenticate for higher rate limits.
authenticateRequestMu
.
RLock
()
authRequest
:=
authenticateRequest
authenticateRequestMu
.
RUnlock
()
if
authRequest
!=
nil
{
authRequest
(
q2
,
h2
)
}
req2
:=
&
http.
Request
{
Method
:
r
.
Method
,
Body
:
r
.
Body
,
URL
:
&
url.
URL
{
Scheme
:
"https"
,
Host
:
"api.github.com"
,
Path
:
r
.
URL
.
Path
,
RawQuery
:
q2
.
Encode
(),
},
Header
:
h2
,
}
requestMu
.
Lock
()
resp
,
err
:=
http
.
DefaultClient
.
Do
(
req2
)
requestMu
.
Unlock
()
if
err
!=
nil
{
log
.
Print
(
err
)
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
return
}
defer
resp
.
Body
.
Close
()
if
limit
:=
resp
.
Header
.
Get
(
"X-Ratelimit-Remaining"
);
limit
!=
""
{
limit
,
_
:=
strconv
.
Atoi
(
limit
)
resource
:=
"core"
if
strings
.
HasPrefix
(
r
.
URL
.
Path
,
"/search/"
) {
resource
=
"search"
}
else
if
r
.
URL
.
Path
==
"/graphql"
{
resource
=
"graphql"
}
rateLimitRemainingGauge
.
WithLabelValues
(
resource
).
Set
(
float64
(
limit
))
}
for
k
,
v
:=
range
resp
.
Header
{
w
.
Header
()[
k
]
=
v
}
w
.
WriteHeader
(
resp
.
StatusCode
)
if
resp
.
StatusCode
<
400
||
!
logRequests
{
io
.
Copy
(
w
,
resp
.
Body
)
return
}
b
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
log15
.
Warn
(
"proxy error"
,
"status"
,
resp
.
StatusCode
,
"body"
,
string
(
b
),
"bodyErr"
,
err
)
io
.
Copy
(
w
,
bytes
.
NewReader
(
b
))
})
if
logRequests
{
h
=
handlers
.
LoggingHandler
(
os
.
Stdout
,
h
)
}
h
=
prometheus
.
InstrumentHandler
(
"github-proxy"
,
h
)
http
.
Handle
(
"/"
,
h
)
host
:=
""
if
env
.
InsecureDev
{
host
=
"127.0.0.1"
}
addr
:=
net
.
JoinHostPort
(
host
,
port
)
log15
.
Info
(
"github-proxy: listening"
,
"addr"
,
addr
)
log
.
Fatal
(
http
.
ListenAndServe
(
addr
,
nil
))
}
Back
|
FazBrowse Home
|
New Git URL