utils.go - fingered - Fingerd protocol daemon, allowing custom responses.
 (HTM) git clone git://jay.scot/fingered
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       utils.go (1215B)
       ---
            1 package utils
            2 
            3 import (
            4         "fmt"
            5         "log"
            6         "net"
            7         "os"
            8         "os/exec"
            9         "unicode"
           10 )
           11 
           12 func LogMsg(format string, args ...interface{}) {
           13         message := fmt.Sprintf(format, args...)
           14         log.Print(message)
           15 }
           16 
           17 func GetContent(filePath string) (string, error) {
           18         _, err := os.Stat(filePath)
           19         if os.IsNotExist(err) {
           20                 return "file not found", err
           21         }
           22 
           23         content, err := os.ReadFile(filePath)
           24         if err != nil {
           25                 return "unable to read file", err
           26         }
           27 
           28         isScript := false
           29         if len(content) > 2 && string(content[:3]) == "#!/" {
           30                 isScript = true
           31         }
           32 
           33         if isScript {
           34                 cmd := exec.Command("sh", filePath)
           35                 output, err := cmd.CombinedOutput()
           36                 if err != nil {
           37                         return "file execution failed", err
           38                 }
           39                 return string(output), nil
           40         }
           41 
           42         return string(content), nil
           43 }
           44 
           45 func WriteResponse(conn net.Conn, response string) (string, error) {
           46         _, err := conn.Write([]byte(response))
           47         if err != nil {
           48                 return "failed to write to socket", err
           49         }
           50 
           51         return "", nil
           52 }
           53 
           54 func IsValidWord(input string) bool {
           55         if len(input) < 1 || len(input) > 32 || !unicode.IsLower(rune(input[0])) {
           56                 return false
           57         }
           58 
           59         for _, r := range input {
           60                 if !(unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_') {
           61                         return false
           62                 }
           63         }
           64 
           65         return input[len(input)-1] != '_'
           66 }