-
Notifications
You must be signed in to change notification settings - Fork 19
/
server.go
121 lines (109 loc) · 3.54 KB
/
server.go
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
// Copyright (C) 2020-2021, IrineSistiana
//
// This file is part of simple-tls.
//
// simple-tls is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// simple-tls is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package core
import (
"crypto/tls"
"errors"
"fmt"
"github.com/IrineSistiana/simple-tls/core/grpc_tunnel"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
"log"
"net"
"time"
)
type Server struct {
BindAddr string
DstAddr string
GRPC bool
GRPCServiceName string
Cert, Key, ServerName string
IdleTimeout time.Duration
OutboundBuf int
InboundBuf int
testListener net.Listener
testCert *tls.Certificate
}
var errMissingCertOrKey = errors.New("one of cert or key argument is missing")
func (s *Server) ActiveAndServe() error {
var l net.Listener
if s.testListener != nil {
l = s.testListener
} else {
var err error
l, err = net.Listen("tcp", s.BindAddr)
if err != nil {
return err
}
}
var certificate tls.Certificate
if s.testCert != nil {
certificate = *s.testCert
} else {
switch {
case len(s.Cert) == 0 && len(s.Key) == 0: // no cert and key
dnsName, _, keyPEM, certPEM, err := GenerateCertificate(s.ServerName, nil)
if err != nil {
return fmt.Errorf("failed to generate temp cert: %w", err)
}
log.Printf("warnning: you are using a tmp certificate with dns name: %s", dnsName)
cer, err := tls.X509KeyPair(certPEM, keyPEM)
if err != nil {
return fmt.Errorf("cannot load x509 key pair from memory: %w", err)
}
certificate = cer
case len(s.Cert) != 0 && len(s.Key) != 0: // has a cert and a key
cer, err := tls.LoadX509KeyPair(s.Cert, s.Key) //load cert
if err != nil {
return fmt.Errorf("cannot load x509 key pair from disk: %w", err)
}
certificate = cer
default:
return errMissingCertOrKey
}
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{certificate},
VerifyConnection: func(state tls.ConnectionState) error {
if state.Version != tls.VersionTLS13 {
return fmt.Errorf("unsafe tls version %d", state.Version)
}
return nil
},
}
if s.GRPC {
grpcServer := grpc.NewServer(
grpc.KeepaliveParams(keepalive.ServerParameters{
MaxConnectionIdle: time.Second * 300,
Time: time.Second * 30,
Timeout: time.Second * 10,
}),
grpc.MaxSendMsgSize(64*1024),
grpc.MaxRecvMsgSize(64*1024),
grpc.Creds(credentials.NewTLS(tlsConfig)),
grpc.InitialWindowSize(64*1024),
grpc.InitialConnWindowSize(64*1024),
grpc.MaxConcurrentStreams(64), // This limit is larger than the hardcoded client limit.
grpc.MaxHeaderListSize(2048),
)
grpc_tunnel.RegisterGRPCTunnelServerAddon(grpcServer, newGrpcServerHandler(s.DstAddr, s.IdleTimeout, s.OutboundBuf), s.GRPCServiceName)
return grpcServer.Serve(l)
}
l = tls.NewListener(l, tlsConfig)
return ListenRawConn(l, NewDstTransportHandler(s.DstAddr, s.IdleTimeout, s.OutboundBuf))
}