Introduction
Introduction Statistics Contact Development Disclaimer Help
main.go - fingered - Fingerd protocol daemon, allowing custom responses.
git clone git://jay.scot/fingered
Log
Files
Refs
README
LICENSE
---
main.go (1668B)
---
1 package main
2
3 import (
4 "fmt"
5 "net"
6 "path/filepath"
7 "strings"
8
9 "fingered/config"
10 "fingered/utils"
11 )
12
13 type Config struct {
14 Threads int
15 Port int
16 Path string
17 }
18
19 const bufferSize = 1024
20
21 func handleRequest(conn net.Conn, dir string, index string) {
22 defer conn.Close()
23
24 // Read the incoming request
25 buffer := make([]byte, bufferSize)
26 n, err := conn.Read(buffer)
27 if err != nil {
28 utils.LogMsg("ERROR: %v", err)
29 return
30 }
31
32 request := strings.TrimSpace(string(buffer[:n]))
33
34 // Sanitize the request
35 if len(request) > 0 && !utils.IsValidWord(request) {
36 utils.LogMsg("INFO: Invalid username")
37 _, err = utils.WriteResponse(conn, "Invaild user\n")
38 if err != nil {
39 utils.LogMsg("ERROR: %s", err)
40 return
41 }
42
43 return
44 }
45
46 if len(request) == 0 {
47 request = index
48 }
49
50 response, err := utils.GetContent(filepath.Join(dir, request))
51 if err != nil {
52 utils.LogMsg("ERROR: %s", err)
53 return
54 }
55
56 _, err = utils.WriteResponse(conn, response)
57 if err != nil {
58 utils.LogMsg("ERROR: %s", err)
59 return
60 }
61
62 }
63
64 func main() {
65
66 cfg := config.ParseFlags()
67
68 connectionChannel := make(chan net.Conn, cfg.Threads)
69
70 for i := 0; i < cfg.Threads; i++ {
71 go func() {
72 for conn := range connectionChannel {
73 handleRequest(conn, cfg.Dir, cfg.Index)
74 }
75 }()
76 }
77
78 listener, err := net.Listen("tcp", fmt.Sprintf(":%d", cfg.Port))
79 if err != nil {
80 utils.LogMsg("ERROR: %v", err)
81 return
82 }
83 defer listener.Close()
84
85 utils.LogMsg("Starting with threads: %d, port: %d, dir: %s", cfg…
86
87 for {
88 conn, err := listener.Accept()
89 if err != nil {
90 utils.LogMsg("ERROR: %v", err)
91 continue
92 }
93 connectionChannel <- conn
94 }
95 }
You are viewing proxied material from jay.scot. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.