forked from projectdiscovery/simplehttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpserver.go
More file actions
145 lines (124 loc) · 2.86 KB
/
tcpserver.go
File metadata and controls
145 lines (124 loc) · 2.86 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package tcpserver
import (
"crypto/tls"
"io/ioutil"
"net"
"time"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/sslcert"
"gopkg.in/yaml.v2"
)
const readTimeout = 5
// Options of the tcp server
type Options struct {
Listen string
TLS bool
Certificate string
Key string
Domain string
rules []Rule
Verbose bool
}
// TCPServer instance
type TCPServer struct {
options *Options
listener net.Listener
}
// New tcp server instance with specified options
func New(options *Options) (*TCPServer, error) {
return &TCPServer{options: options}, nil
}
// AddRule to the server
func (t *TCPServer) AddRule(rule Rule) error {
t.options.rules = append(t.options.rules, rule)
return nil
}
// ListenAndServe requests
func (t *TCPServer) ListenAndServe() error {
listener, err := net.Listen("tcp4", t.options.Listen)
if err != nil {
return err
}
t.listener = listener
return t.run()
}
func (t *TCPServer) handleConnection(conn net.Conn) error {
defer conn.Close() //nolint
buf := make([]byte, 4096)
for {
if err := conn.SetReadDeadline(time.Now().Add(readTimeout * time.Second)); err != nil {
gologger.Info().Msgf("%s\n", err)
}
_, err := conn.Read(buf)
if err != nil {
return err
}
gologger.Print().Msgf("%s\n", buf)
resp, err := t.BuildResponse(buf)
if err != nil {
return err
}
if _, err := conn.Write(resp); err != nil {
gologger.Info().Msgf("%s\n", err)
}
gologger.Print().Msgf("%s\n", resp)
}
}
// ListenAndServeTLS requests over tls
func (t *TCPServer) ListenAndServeTLS() error {
var tlsConfig *tls.Config
if t.options.Certificate != "" && t.options.Key != "" {
cert, err := tls.LoadX509KeyPair(t.options.Certificate, t.options.Key)
if err != nil {
return err
}
tlsConfig = &tls.Config{Certificates: []tls.Certificate{cert}}
} else {
tlsOptions := sslcert.DefaultOptions
tlsOptions.Host = t.options.Domain
cfg, err := sslcert.NewTLSConfig(tlsOptions)
if err != nil {
return err
}
tlsConfig = cfg
}
listener, err := tls.Listen("tcp", t.options.Listen, tlsConfig)
if err != nil {
return err
}
t.listener = listener
return t.run()
}
func (t *TCPServer) run() error {
for {
c, err := t.listener.Accept()
if err != nil {
return err
}
go t.handleConnection(c) //nolint
}
}
// Close the service
func (t *TCPServer) Close() error {
return t.listener.Close()
}
// LoadTemplate from yaml
func (t *TCPServer) LoadTemplate(templatePath string) error {
var config RulesConfiguration
yamlFile, err := ioutil.ReadFile(templatePath)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
return err
}
for _, ruleTemplate := range config.Rules {
rule, err := NewRule(ruleTemplate.Match, ruleTemplate.Response)
if err != nil {
return err
}
t.options.rules = append(t.options.rules, *rule)
}
return nil
}