-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
60 lines (49 loc) · 1.5 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
package main
import (
"context"
"fmt"
"io"
"net"
"net/http"
"time"
"github.com/libp2p/go-yamux/v4"
_ "github.com/libp2p/go-yamux/v4"
"github.com/pubgo/funk/assert"
logger "github.com/pubgo/funk/log"
"google.golang.org/grpc"
)
func main2() {
listener := assert.Must1(net.Listen("tcp", ":8888"))
// Accept a TCP connection
conn := assert.Must1(listener.Accept())
// Setup server side of yamux
session := assert.Must1(yamux.Client(conn, nil, nil))
// 验证密码,获取服务信息,版本信息
// 验证通过之后才能真正的建立连接
sss := assert.Must1(session.AcceptStream())
dd := make([]byte, 1024)
n := assert.Must1(sss.Read(dd))
fmt.Println(string(dd[:n]))
logger.Info().Msg("session")
// Accept a stream
cli := http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
stream := assert.Must1(session.OpenStream(context.Background()))
return stream, nil
},
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
req := assert.Must1(http.NewRequest("GET", "http://localhost:8080/hello", nil))
rsp := assert.Must1(cli.Do(req))
fmt.Println(string(assert.Must1(io.ReadAll(rsp.Body))))
connCli := assert.Must1(grpc.Dial("test:8080", grpc.WithContextDialer(func(ctx context.Context, s string) (net.Conn, error) {
return session.Open(ctx)
})))
_ = connCli
}