mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
general: optimize codes
This commit is contained in:
1 parent
3c509f8b7a
commit
8f661a67a4
15 files changed
+35
-37
No files matched your search
@@ -40,7 +40,7 @@ func (s *HTTP) Dial(network, addr string) (net.Conn, error) {
|
||||
|
||||
if s.user != "" && s.password != "" {
|
||||
auth := s.user + ":" + s.password
|
||||
buf.Write([]byte("Proxy-Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) + "\r\n"))
|
||||
buf.WriteString("Proxy-Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) + "\r\n")
|
||||
}
|
||||
|
||||
// header ended
|
||||
|
||||
+3
-3
@@ -82,16 +82,16 @@ func cleanHeaders(header textproto.MIMEHeader) {
|
||||
}
|
||||
|
||||
func writeStartLine(w io.Writer, s1, s2, s3 string) {
|
||||
w.Write([]byte(s1 + " " + s2 + " " + s3 + "\r\n"))
|
||||
io.WriteString(w, s1+" "+s2+" "+s3+"\r\n")
|
||||
}
|
||||
|
||||
func writeHeaders(w io.Writer, header textproto.MIMEHeader) {
|
||||
for key, values := range header {
|
||||
for _, v := range values {
|
||||
w.Write([]byte(key + ": " + v + "\r\n"))
|
||||
io.WriteString(w, key+": "+v+"\r\n")
|
||||
}
|
||||
}
|
||||
w.Write([]byte("\r\n"))
|
||||
io.WriteString(w, "\r\n")
|
||||
}
|
||||
|
||||
func extractUserPass(auth string) (username, password string, ok bool) {
|
||||
|
||||
@@ -75,7 +75,7 @@ func (s *HTTP) servRequest(req *request, c *conn.Conn) {
|
||||
// Auth
|
||||
if s.user != "" && s.password != "" {
|
||||
if user, pass, ok := extractUserPass(req.auth); !ok || user != s.user || pass != s.password {
|
||||
c.Write([]byte("HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic\r\n\r\n"))
|
||||
io.WriteString(c, "HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic\r\n\r\n")
|
||||
log.F("[http] auth failed from %s, auth info: %s:%s", c.RemoteAddr(), user, pass)
|
||||
return
|
||||
}
|
||||
@@ -92,13 +92,13 @@ func (s *HTTP) servRequest(req *request, c *conn.Conn) {
|
||||
func (s *HTTP) servHTTPS(r *request, c net.Conn) {
|
||||
rc, dialer, err := s.proxy.Dial("tcp", r.uri)
|
||||
if err != nil {
|
||||
c.Write([]byte(r.proto + " 502 ERROR\r\n\r\n"))
|
||||
io.WriteString(c, r.proto+" 502 ERROR\r\n\r\n")
|
||||
log.F("[http] %s <-> %s [c] via %s, error in dial: %v", c.RemoteAddr(), r.uri, dialer.Addr(), err)
|
||||
return
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
c.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n"))
|
||||
io.WriteString(c, "HTTP/1.1 200 Connection established\r\n\r\n")
|
||||
|
||||
log.F("[http] %s <-> %s [c] via %s", c.RemoteAddr(), r.uri, dialer.Addr())
|
||||
|
||||
|
||||
Reference in new issue
Block a user