-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
126 lines (111 loc) · 2.77 KB
/
main.go
File metadata and controls
126 lines (111 loc) · 2.77 KB
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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"time"
"github.com/faceair/betproxy"
"github.com/faceair/betproxy/mitm"
"github.com/faceair/gotit/git"
)
func main() {
var host, capath, gopath string
var port int
flag.StringVar(&capath, "capath", os.TempDir(), "path to save gotit certificate")
flag.StringVar(&gopath, "gopath", os.Getenv("GOPATH"), "path to save git repository")
flag.StringVar(&host, "host", "0.0.0.0", "proxy server host")
flag.IntVar(&port, "port", 0, "proxy server port")
flag.Parse()
if port == 0 {
flag.PrintDefaults()
os.Exit(0)
}
certpath := path.Join(capath, "gotit.cert.pem")
keypath := path.Join(capath, "gotit.key.pem")
cacert, cakey, err := loadCA(certpath, keypath)
if err != nil {
panic(err)
}
tlsCfg, err := mitm.NewConfig(cacert, cakey)
if err != nil {
panic(err)
}
if err != nil {
panic(err)
}
service, err := betproxy.NewService(fmt.Sprintf("%s:%d", host, port), tlsCfg)
if err != nil {
panic(err)
}
service.SetClient(git.NewServer(gopath, certpath))
log.Fatal(service.Listen())
}
func loadCA(certpath, keypath string) (*x509.Certificate, *rsa.PrivateKey, error) {
if _, err := os.Stat(certpath); os.IsNotExist(err) {
err := generateCA(certpath, keypath)
if err != nil {
return nil, nil, err
}
}
cert, err := ioutil.ReadFile(certpath)
if err != nil {
return nil, nil, err
}
key, err := ioutil.ReadFile(keypath)
if err != nil {
return nil, nil, err
}
certBlock, _ := pem.Decode(cert)
if certBlock == nil {
return nil, nil, errors.New("Failed to decode ca certificate")
}
keyBlock, _ := pem.Decode(key)
if keyBlock == nil {
return nil, nil, errors.New("Failed to decode ca private key")
}
rawKey, err := x509.ParsePKCS1PrivateKey(keyBlock.Bytes)
if err != nil {
return nil, nil, err
}
rawCert, err := x509.ParseCertificate(certBlock.Bytes)
if err != nil {
return nil, nil, err
}
return rawCert, rawKey, nil
}
func generateCA(certpath, keypath string) error {
cacert, cakey, err := mitm.NewAuthority("gotit", "faceair", 10*365*24*time.Hour)
if err != nil {
return err
}
derBytes, err := x509.CreateCertificate(rand.Reader, cacert, cacert, &cakey.PublicKey, cakey)
if err != nil {
return err
}
certOut, err := os.Create(certpath)
if err != nil {
return err
}
if err = pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
return err
}
if err = certOut.Close(); err != nil {
return err
}
keyOut, err := os.OpenFile(keypath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
if err = pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(cakey)}); err != nil {
return err
}
return keyOut.Close()
}