-
Notifications
You must be signed in to change notification settings - Fork 11
/
echoserver.go
142 lines (114 loc) · 2.98 KB
/
echoserver.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package netserver
import (
"crypto/tls"
"fmt"
"io"
"net"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddytls"
)
// EchoServer is an echo implementation of the
// caddy.Server interface type
type EchoServer struct {
LocalTCPAddr string
tcpListener net.Listener
udpListener net.PacketConn
udpSemaphore chan int
config *Config
}
// NewEchoServer returns a new echo server
func NewEchoServer(l string, c *Config) (*EchoServer, error) {
return &EchoServer{
LocalTCPAddr: l,
udpSemaphore: make(chan int, 100),
config: c,
}, nil
}
// Listen starts listening by creating a new listener
// and returning it. It does not start accepting
// connections.
func (s *EchoServer) Listen() (net.Listener, error) {
var listener net.Listener
tlsConfig, err := caddytls.MakeTLSConfig([]*caddytls.Config{s.config.TLS})
if err != nil {
return nil, err
}
inner, err := net.Listen("tcp", fmt.Sprintf("%s", s.LocalTCPAddr))
if err != nil {
return nil, err
}
if tlsConfig != nil {
listener = tls.NewListener(inner, tlsConfig)
} else {
listener = inner
}
return listener, nil
}
// ListenPacket starts listening by creating a new Packet listener
// and returning it. It does not start accepting
// connections.
func (s *EchoServer) ListenPacket() (net.PacketConn, error) {
return net.ListenPacket("udp", fmt.Sprintf("%s", s.LocalTCPAddr))
}
// Serve starts serving using the provided listener.
// Serve blocks indefinitely, or in other
// words, until the server is stopped.
func (s *EchoServer) Serve(ln net.Listener) error {
s.tcpListener = ln
for {
conn, err := ln.Accept()
if err != nil {
return err
}
go func(c net.Conn) {
// Echo all incoming data.
_, err := io.Copy(c, c)
if err != nil {
fmt.Printf("io.Copy error: %v\n", err)
}
// Shut down the connection.
c.Close()
}(conn)
}
}
// ServePacket starts serving using the provided listener.
// ServePacket blocks indefinitely, or in other
// words, until the server is stopped.
func (s *EchoServer) ServePacket(con net.PacketConn) error {
s.udpListener = con
for {
s.udpSemaphore <- 1 //semaphore
go s.echoUDP(con)
}
}
func (s *EchoServer) echoUDP(con net.PacketConn) {
defer func() { <-s.udpSemaphore }()
buf := make([]byte, 4096)
nr, addr, err := con.ReadFrom(buf)
if err != nil {
s.udpListener.Close()
}
_, err = con.WriteTo(buf[:nr], addr)
if err != nil {
s.udpListener.Close()
}
}
// Stop stops s gracefully and closes its listener.
func (s *EchoServer) Stop() error {
err := s.tcpListener.Close()
if err != nil {
return err
}
err = s.udpListener.Close()
if err != nil {
return err
}
return nil
}
// OnStartupComplete lists the sites served by this server
// and any relevant information
func (s *EchoServer) OnStartupComplete() {
if !caddy.Quiet {
fmt.Println("[INFO] Echoing on port ", s.LocalTCPAddr)
}
}