Skip to content

Commit 1fe30e1

Browse files
committed
adding comments
1 parent 566a82a commit 1fe30e1

File tree

9 files changed

+31
-5
lines changed

9 files changed

+31
-5
lines changed

cmd/simplehttpserver/simplehttpserver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
func main() {
99
// Parse the command line flags and read config files
1010
options := runner.ParseOptions()
11-
runner, err := runner.New(options)
11+
r, err := runner.New(options)
1212
if err != nil {
1313
gologger.Fatal().Msgf("Could not create runner: %s\n", err)
1414
}
1515

16-
runner.Run()
17-
runner.Close()
16+
r.Run()
17+
defer r.Close()
1818
}

internal/runner/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func (options *Options) configureOutput() {
9393
}
9494
}
9595

96+
// FolderAbsPath of the fileserver folder
9697
func (o Options) FolderAbsPath() string {
9798
abspath, err := filepath.Abs(o.Folder)
9899
if err != nil {

internal/runner/runner.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Runner struct {
1414
httpServer *httpserver.HTTPServer
1515
}
1616

17+
// New instance of runner
1718
func New(options *Options) (*Runner, error) {
1819
r := Runner{options: options}
1920
// Check if the process can listen on the specified ip:port
@@ -65,6 +66,7 @@ func New(options *Options) (*Runner, error) {
6566
return &r, nil
6667
}
6768

69+
// Run logic
6870
func (r *Runner) Run() error {
6971
if r.options.EnableTCP {
7072
gologger.Print().Msgf("Serving TCP rule based server on tcp://%s", r.options.ListenAddress)
@@ -80,6 +82,7 @@ func (r *Runner) Run() error {
8082
return r.httpServer.ListenAndServe()
8183
}
8284

85+
// Close the listening services
8386
func (r *Runner) Close() error {
8487
if r.serverTCP != nil {
8588
r.serverTCP.Close()

pkg/binder/binder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/phayes/freeport"
88
)
99

10+
// CanListenOn the specified address
1011
func CanListenOn(address string) bool {
1112
listener, err := net.Listen("tcp4", address)
1213
if err != nil {
@@ -16,6 +17,7 @@ func CanListenOn(address string) bool {
1617
return true
1718
}
1819

20+
// GetRandomListenAddress from the specified one
1921
func GetRandomListenAddress(currentAddress string) (string, error) {
2022
addrOrig, _, err := net.SplitHostPort(currentAddress)
2123
if err != nil {

pkg/httpserver/httpserver.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/projectdiscovery/sslcert"
77
)
88

9+
// Options of the http server
910
type Options struct {
1011
Folder string
1112
EnableUpload bool
@@ -20,11 +21,13 @@ type Options struct {
2021
Verbose bool
2122
}
2223

24+
// HTTPServer instance
2325
type HTTPServer struct {
2426
options *Options
2527
layers http.Handler
2628
}
2729

30+
// New http server instance with options
2831
func New(options *Options) (*HTTPServer, error) {
2932
var h HTTPServer
3033
EnableUpload = options.EnableUpload
@@ -37,10 +40,12 @@ func New(options *Options) (*HTTPServer, error) {
3740
return &HTTPServer{options: options, layers: layers}, nil
3841
}
3942

43+
// ListenAndServe requests over http
4044
func (t *HTTPServer) ListenAndServe() error {
4145
return http.ListenAndServe(t.options.ListenAddress, t.layers)
4246
}
4347

48+
// ListenAndServeTLS requests over https
4449
func (t *HTTPServer) ListenAndServeTLS() error {
4550
if t.options.Certificate == "" || t.options.CertificateKey == "" {
4651
tlsOptions := sslcert.DefaultOptions
@@ -59,6 +64,7 @@ func (t *HTTPServer) ListenAndServeTLS() error {
5964
return http.ListenAndServeTLS(t.options.ListenAddress, t.options.Certificate, t.options.CertificateKey, t.layers)
6065
}
6166

67+
// Close the service
6268
func (t *HTTPServer) Close() error {
6369
return nil
6470
}

pkg/httpserver/loglayer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,18 @@ func newLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
5454
return &loggingResponseWriter{w, http.StatusOK, []byte{}}
5555
}
5656

57+
// Write the data
5758
func (lrw *loggingResponseWriter) Write(data []byte) (int, error) {
5859
lrw.Data = append(lrw.Data, data...)
5960
return lrw.ResponseWriter.Write(data)
6061
}
6162

63+
// Header of the response
6264
func (lrw *loggingResponseWriter) Header() http.Header {
6365
return lrw.ResponseWriter.Header()
6466
}
6567

68+
// WriteHeader status code
6669
func (lrw *loggingResponseWriter) WriteHeader(code int) {
6770
lrw.statusCode = code
6871
lrw.ResponseWriter.WriteHeader(code)

pkg/tcpserver/responseengine.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"errors"
55
)
66

7+
// BuildResponse according to rules
78
func (t *TCPServer) BuildResponse(data []byte) ([]byte, error) {
89
// Process all the rules
910
for _, rule := range t.options.rules {
1011
if rule.matchRegex.Match(data) {
1112
return []byte(rule.Response), nil
1213
}
1314
}
14-
return nil, errors.New("No matched rule")
15+
return nil, errors.New("no matched rule")
1516
}

pkg/tcpserver/rule.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ package tcpserver
22

33
import "regexp"
44

5+
// RulesConfiguration from yaml
56
type RulesConfiguration struct {
67
Rules []Rule `yaml:"rules"`
78
}
89

10+
// Rule to apply to various requests
911
type Rule struct {
1012
Match string `yaml:"match,omitempty"`
1113
matchRegex *regexp.Regexp
1214
Response string `yaml:"response,omitempty"`
1315
}
1416

15-
func NewRule(match string, response string) (*Rule, error) {
17+
// NewRule from model
18+
func NewRule(match, response string) (*Rule, error) {
1619
regxp, err := regexp.Compile(match)
1720
if err != nil {
1821
return nil, err

pkg/tcpserver/tcpserver.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"gopkg.in/yaml.v2"
1212
)
1313

14+
// Options of the tcp server
1415
type Options struct {
1516
Listen string
1617
TLS bool
@@ -21,20 +22,24 @@ type Options struct {
2122
Verbose bool
2223
}
2324

25+
// TCPServer instance
2426
type TCPServer struct {
2527
options Options
2628
listener net.Listener
2729
}
2830

31+
// New tcp server instance with specified options
2932
func New(options Options) (*TCPServer, error) {
3033
return &TCPServer{options: options}, nil
3134
}
3235

36+
// AddRule to the server
3337
func (t *TCPServer) AddRule(rule Rule) error {
3438
t.options.rules = append(t.options.rules, rule)
3539
return nil
3640
}
3741

42+
// ListenAndServe requests
3843
func (t *TCPServer) ListenAndServe() error {
3944
listener, err := net.Listen("tcp4", t.options.Listen)
4045
if err != nil {
@@ -68,6 +73,7 @@ func (t *TCPServer) handleConnection(conn net.Conn) error {
6873
}
6974
}
7075

76+
// ListenAndServe requests over tls
7177
func (t *TCPServer) ListenAndServeTLS() error {
7278
var tlsConfig *tls.Config
7379
if t.options.Certificate != "" && t.options.Key != "" {
@@ -108,6 +114,7 @@ func (t *TCPServer) Close() error {
108114
return t.listener.Close()
109115
}
110116

117+
// LoadTemplate from yaml
111118
func (t *TCPServer) LoadTemplate(templatePath string) error {
112119
var config RulesConfiguration
113120
yamlFile, err := ioutil.ReadFile(templatePath)

0 commit comments

Comments
 (0)