Skip to content

Commit d426a6c

Browse files
committed
update to v0.2.2
support tls cert and key autogen
1 parent fe68e94 commit d426a6c

File tree

3 files changed

+62
-30
lines changed

3 files changed

+62
-30
lines changed

status.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,19 @@ import (
77
. "github.com/lxn/walk/declarative"
88
)
99

10-
var dataFlow *walk.StatusBarItem
11-
var sessionFlow *walk.StatusBarItem
1210
var requestFlow *walk.StatusBarItem
1311

1412
func StatusRequestUpdate(cnt int64) {
1513
if requestFlow != nil {
16-
requestFlow.SetText(fmt.Sprintf("REQUEST: %d", cnt))
17-
}
18-
}
19-
20-
func StatusSessionUpdate(cnt int64) {
21-
if sessionFlow != nil {
22-
sessionFlow.SetText(fmt.Sprintf("SESSION: %d", cnt))
23-
}
24-
}
25-
26-
func StatusFlowUpdate(flow int64) {
27-
if dataFlow != nil {
28-
dataFlow.SetText(fmt.Sprintf("DATAFLOW: %s", ByteView(flow)))
14+
requestFlow.SetText(fmt.Sprintf(" REQUEST: %d", cnt))
2915
}
3016
}
3117

3218
func StatusBarInit() []StatusBarItem {
3319
return []StatusBarItem{
34-
{
35-
AssignTo: &dataFlow,
36-
Text: "",
37-
Width: 120,
38-
},
39-
{
40-
AssignTo: &sessionFlow,
41-
Text: "",
42-
Width: 120,
43-
},
4420
{
4521
AssignTo: &requestFlow,
46-
Text: "",
22+
Text: " REQUEST: 0",
4723
Width: 120,
4824
},
4925
}

tls.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TlsAction() {
6161
HSpacer{},
6262
PushButton{
6363
AssignTo: &acceptPB,
64-
Text: "OK",
64+
Text: "Save",
6565
OnClicked: func() {
6666
if tlsCert.Text() != "" || tlsKey.Text() != "" {
6767
_, err := CreateTlsConfig(tlsCert.Text(), tlsKey.Text())
@@ -85,6 +85,15 @@ func TlsAction() {
8585
},
8686
},
8787
HSpacer{},
88+
PushButton{
89+
Text: "Generate",
90+
OnClicked: func() {
91+
cert, key := GenerateKeyCert(ConfigGet().ListenAddr)
92+
tlsCert.SetText(cert)
93+
tlsKey.SetText(key)
94+
},
95+
},
96+
HSpacer{},
8897
PushButton{
8998
AssignTo: &cancelPB,
9099
Text: "Cancel",

util.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
package main
22

33
import (
4+
"bytes"
5+
"crypto/rand"
6+
"crypto/rsa"
47
"crypto/tls"
8+
"crypto/x509"
9+
"crypto/x509/pkix"
10+
"encoding/pem"
511
"fmt"
6-
"math/rand"
12+
"math/big"
713
"net"
814
"os"
915
"os/signal"
1016
"syscall"
17+
"time"
1118

1219
"github.com/astaxie/beego/logs"
1320
"github.com/lxn/walk"
21+
22+
mrand "math/rand"
1423
)
1524

1625
func VersionGet() string {
17-
return "v0.2.1"
26+
return "v0.2.2"
1827
}
1928

2029
func SaveToFile(name string, body []byte) error {
@@ -94,7 +103,7 @@ func GenerateUsername(length int) string {
94103
charSet := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*_+-="
95104
username := make([]byte, length)
96105
for i := range username {
97-
index := rand.Intn(len(charSet))
106+
index := mrand.Intn(len(charSet))
98107
username[i] = charSet[index]
99108
}
100109
return string(username)
@@ -129,3 +138,41 @@ func CreateTlsConfig(cert, key string) (*tls.Config, error) {
129138
ClientAuth: tls.RequestClientCert,
130139
}, nil
131140
}
141+
142+
func GenerateKeyCert(addr string) (string, string) {
143+
max := new(big.Int).Lsh(big.NewInt(1), 128)
144+
serialNumber, _ := rand.Int(rand.Reader, max)
145+
subject := pkix.Name{
146+
Organization: []string{"simple http server windows app"},
147+
OrganizationalUnit: []string{"simple http server windows app"},
148+
CommonName: "simple http server windows app",
149+
}
150+
151+
if addr == "0.0.0.0" || addr == "::" {
152+
addr = "127.0.0.1"
153+
}
154+
155+
ipAddress := make([]net.IP, 0)
156+
ipAddress = append(ipAddress, net.ParseIP(addr))
157+
158+
template := x509.Certificate{
159+
SerialNumber: serialNumber,
160+
Subject: subject,
161+
NotBefore: time.Now(),
162+
NotAfter: time.Now().Add(30 * 24 * time.Hour),
163+
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
164+
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
165+
IPAddresses: ipAddress,
166+
}
167+
pk, _ := rsa.GenerateKey(rand.Reader, 2048)
168+
169+
derBytes, _ := x509.CreateCertificate(rand.Reader, &template, &template, &pk.PublicKey, pk)
170+
171+
certOut := bytes.NewBuffer(make([]byte, 0))
172+
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
173+
174+
keyOut := bytes.NewBuffer(make([]byte, 0))
175+
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(pk)})
176+
177+
return certOut.String(), keyOut.String()
178+
}

0 commit comments

Comments
 (0)