Skip to content

Commit

Permalink
user specify the certificate path
Browse files Browse the repository at this point in the history
  • Loading branch information
ls0f committed Jul 19, 2017
1 parent 152955a commit bdf57bd
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,14 @@ It is strongly recommended to open the https option on the server side.

### Notice

The file name of certificate and private key must be `cert.pem` and `key.pem` and with the server bin under the same folder.

If you have a ssl certificate, It would be easy.

copy the certificate and private key into the same folder with server bin

```
./server -addr :8080 -secret <password> -https
./server -addr :443 -secret <password> -https -cert /etc/cert.pem -key /etc/key.pem
```

```
./local -raddr https://example.com:8080 -secret <password>
./local -raddr https://example.com -secret <password>
```

Of Course, you can create a self-signed ssl certificate by openssl.
Expand All @@ -68,12 +64,11 @@ sh -c "$(curl https://raw.githubusercontent.com/lovedboy/cracker/master/gen_key_
```

```
./server -addr :8080 -secret <password> -https
./server -addr :443 -secret <password> -https -cert /etc/self-signed-cert.pem -key /etc/self-ca-key.pem
```
copy the certificate into the same folder with local bin.

```
./local -raddr https://<ip>:8080 -secret <password>
./local -raddr https://example.com -secret <password> -cert /etc/self-signed-cert.pem
```


Expand Down
22 changes: 12 additions & 10 deletions cracker/proxy/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,23 @@ func NewHttpProxy(addr, secret string, https bool) *httpProxy {
}
}

func (hp *httpProxy) Listen() {
func (hp *httpProxy) handler() {
http.HandleFunc(CONNECT, hp.connect)
http.HandleFunc(PULL, hp.pull)
http.HandleFunc(PUSH, hp.push)
http.HandleFunc(PING, hp.ping)
}

func (hp *httpProxy) ListenHTTPS(cert, key string) {
hp.handler()
g.Infof("listen at:[%s]", hp.addr)
var err error
if hp.https {
err = http.ListenAndServeTLS(hp.addr, "cert.pem", "key.pem", nil)
} else {
err = http.ListenAndServe(hp.addr, nil)
}
if err != nil {
g.Fatal("ListenAndServe: ", err)
}
g.Fatal("ListenAndServe: ", http.ListenAndServeTLS(hp.addr, cert, key, nil))
}

func (hp *httpProxy) Listen() {
hp.handler()
g.Infof("listen at:[%s]", hp.addr)
g.Fatal("ListenAndServe: ", http.ListenAndServe(hp.addr, nil))
}

func (hp *httpProxy) verify(r *http.Request) error {
Expand Down
8 changes: 4 additions & 4 deletions cracker/proxy/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ var tr = &http.Transport{
Proxy: http.ProxyFromEnvironment,
}

func Init() {
if f, err := os.Stat("cert.pem"); err == nil && !f.IsDir() {
func Init(cert string) {
if f, err := os.Stat(cert); err == nil && !f.IsDir() {
var CAPOOL *x509.CertPool
CAPOOL, err := x509.SystemCertPool()
if err != nil {
g.Warning(err)
CAPOOL = x509.NewCertPool()
}
serverCert, err := ioutil.ReadFile("cert.pem")
serverCert, err := ioutil.ReadFile(cert)
if err != nil {
g.Errorf("read cert.pem err:%s ", err)
return
}
CAPOOL.AppendCertsFromPEM(serverCert)
config := &tls.Config{RootCAs: CAPOOL}
tr.TLSClientConfig = config
g.Info("load cert.pem success ... ")
g.Infof("load %s success ... ", cert)
}
}

Expand Down
5 changes: 4 additions & 1 deletion local/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func main() {
secret := flag.String("secret", "", "secret key")
debug := flag.Bool("debug", false, "debug mode")
version := flag.Bool("v", false, "version")
cert := flag.String("cert", "", "cert file")
flag.Parse()

if *version {
Expand All @@ -31,7 +32,9 @@ func main() {
os.Exit(0)
}
logger.InitLogger(*debug)
proxy.Init()
if *cert != "" {
proxy.Init(*cert)
}
s, err := NewLocalProxyServer(*addr, *raddr, *secret)
if err != nil {
g.Fatal(err)
Expand Down
16 changes: 10 additions & 6 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,34 @@ func main() {
debug := flag.Bool("debug", false, "debug mode")
version := flag.Bool("v", false, "version")
https := flag.Bool("https", false, "https")
cert := flag.String("cert", "", "cert file")
key := flag.String("key", "", "private key file")
flag.Parse()
logger.InitLogger(*debug)
if *version {
fmt.Printf("GitTag: %s \n", GitTag)
fmt.Printf("BuildTime: %s \n", BuildTime)
os.Exit(0)
}
p := proxy.NewHttpProxy(*addr, *secret, *https)
if *https {
f, err := os.Stat("cert.pem")
f, err := os.Stat(*cert)
if err != nil {
g.Fatal(err)
}
if f.IsDir() {
g.Fatal("cert.pem should be file")
g.Fatal("cert should be file")
}
f, err = os.Stat("key.pem")
f, err = os.Stat(*key)
if err != nil {
g.Fatal(err)
}
if f.IsDir() {
g.Fatal("key.pem should be file")
g.Fatal("key should be file")
}
p.ListenHTTPS(*cert, *key)
} else {
p.Listen()
}
p := proxy.NewHttpProxy(*addr, *secret, *https)
p.Listen()

}

0 comments on commit bdf57bd

Please sign in to comment.