Skip to content

Commit

Permalink
Fix explicit proxy hang
Browse files Browse the repository at this point in the history
ex. docker pull gcr.io/google_containers/pause-amd64:3.0
  • Loading branch information
wadahiro committed Feb 7, 2018
1 parent 2bc35b3 commit 36a8c09
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 40 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

VERSION=0.5
VERSION=0.5.1

DIR=$(cd $(dirname $0); pwd)
cd $DIR
Expand Down
38 changes: 19 additions & 19 deletions cmd/transproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,29 @@ func main() {
// seed the global random number generator, used in secureoperator
rand.Seed(time.Now().UTC().UnixNano())

// setup logger
colog.SetDefaultLevel(colog.LDebug)
colog.SetMinLevel(colog.LInfo)
level, err := colog.ParseLevel(*loglevel)
if err != nil {
log.Fatalf("alert: Invalid log level: %s", err.Error())
}
colog.SetMinLevel(level)
colog.SetFormatter(&colog.StdFormatter{
Colors: true,
Flag: log.Ldate | log.Ltime | log.Lmicroseconds,
})
colog.ParseFields(true)
colog.Register()

if *explicitProxyOnly {
startExplicitProxyOnly()
startExplicitProxyOnly(level)
} else {
startAllProxy()
startAllProxy(level)
}
}

func startExplicitProxyOnly() {
func startExplicitProxyOnly(level colog.Level) {
startExplicitProxy()

// serve until exit
Expand All @@ -117,22 +132,7 @@ func startExplicitProxyOnly() {
log.Printf("info: go-transproxy exited.")
}

func startAllProxy() {
// setup logger
colog.SetDefaultLevel(colog.LDebug)
colog.SetMinLevel(colog.LInfo)
level, err := colog.ParseLevel(*loglevel)
if err != nil {
log.Fatalf("alert: Invalid log level: %s", err.Error())
}
colog.SetMinLevel(level)
colog.SetFormatter(&colog.StdFormatter{
Colors: true,
Flag: log.Ldate | log.Ltime | log.Lmicroseconds,
})
colog.ParseFields(true)
colog.Register()

func startAllProxy(level colog.Level) {
// handling no_proxy environment
noProxy := os.Getenv("no_proxy")
if noProxy == "" {
Expand Down
62 changes: 42 additions & 20 deletions explicit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"os"
"strings"
"time"
)

type ExplicitProxy struct {
Expand Down Expand Up @@ -39,11 +40,14 @@ func (s ExplicitProxy) Start() error {
if err != nil {
return err
}
s.user = u.User.Username()

if s.UseProxyAuthorization {
s.category = "Explicit-Proxy(Auth)"

if u.User == nil {
log.Printf("info: Not Started because of no proxy user category='%s'", s.category)
return nil
}
// For HTTPS
s.proxyAuthorization = "Basic " + base64.StdEncoding.EncodeToString([]byte(u.User.String()))
s.proxyHost = u.Host
Expand Down Expand Up @@ -104,26 +108,44 @@ func (s ExplicitProxy) accessLog(r *http.Request) {
}

func (s ExplicitProxy) handleHttps(w http.ResponseWriter, r *http.Request) {
hj, _ := w.(http.Hijacker)
if proxyConn, err := net.Dial("tcp", s.proxyHost); err != nil {
log.Printf("error: %s", err)
} else if clientConn, _, err := hj.Hijack(); err != nil {
proxyConn.Close()
log.Printf("error: %s", err)
} else {
if s.UseProxyAuthorization {
r.Header.Set("Proxy-Authorization", s.proxyAuthorization)
}
r.Write(proxyConn)
go func() {
io.Copy(clientConn, proxyConn)
proxyConn.Close()
}()
go func() {
io.Copy(proxyConn, clientConn)
clientConn.Close()
}()
destConn, err := net.DialTimeout("tcp", s.proxyHost, 10*time.Second)
if err != nil {
log.Printf("error: %s category='%s'", err, s.category)
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}

hj, ok := w.(http.Hijacker)
if !ok {
log.Printf("error: Hijacking not supported category='%s'", s.category)
http.Error(w, "Hijacking not supported", http.StatusInternalServerError)
destConn.Close()
return
}
clientConn, _, err := hj.Hijack()
if err != nil {
log.Printf("error: %s category='%s'", err, s.category)
http.Error(w, err.Error(), http.StatusServiceUnavailable)
destConn.Close()
return
}

if s.UseProxyAuthorization {
r.Header.Set("Proxy-Authorization", s.proxyAuthorization)
}

r.Write(destConn)

go transfer(clientConn, destConn)
go transfer(destConn, clientConn)

log.Printf("debug: End proxy category='%s'", s.category)
}

func transfer(destination io.WriteCloser, source io.ReadCloser) {
defer destination.Close()
defer source.Close()
io.Copy(destination, source)
}

func (s ExplicitProxy) handleHttp(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 36a8c09

Please sign in to comment.