go package folders
This commit is contained in:
32
cmd/http/httpsocket.go
Normal file
32
cmd/http/httpsocket.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type ConfigTOML struct {
|
||||
Config struct {
|
||||
DefaultHttpPort int `toml:"httpPort"`
|
||||
DefaultCacheDir string `toml:"cacheDir"`
|
||||
} `toml:"Config"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
var cfg ConfigTOML
|
||||
toml.Decode("opt/packets/packets/config.toml", &cfg)
|
||||
|
||||
pid := os.Getpid()
|
||||
if err := os.WriteFile("./http.pid", []byte(fmt.Sprint(pid)), 0644); err != nil {
|
||||
fmt.Println("error saving subprocess pid", err)
|
||||
}
|
||||
|
||||
fs := http.FileServer(http.Dir(cfg.Config.DefaultCacheDir))
|
||||
http.Handle("/", fs)
|
||||
|
||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", cfg.Config.DefaultHttpPort), nil))
|
||||
}
|
||||
1267
cmd/packets/main.go
Normal file
1267
cmd/packets/main.go
Normal file
File diff suppressed because it is too large
Load Diff
51
cmd/udp/udpsocket.go
Normal file
51
cmd/udp/udpsocket.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func CheckDownloaded(filename string) bool {
|
||||
|
||||
_, err := os.Stat(fmt.Sprintf("/var/cache/packets/%s", filename))
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
pid := os.Getpid()
|
||||
if err := os.WriteFile("/opt/packets/packets/udp.pid", []byte(fmt.Sprint(pid)), 0644); err != nil {
|
||||
fmt.Println("error saving subprocess pid", err)
|
||||
}
|
||||
|
||||
addr := net.UDPAddr{IP: net.IPv4zero, Port: 1333}
|
||||
conn, err := net.ListenUDP("udp", &addr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
buf := make([]byte, 1500)
|
||||
|
||||
for {
|
||||
n, remote, err := conn.ReadFromUDP(buf)
|
||||
if err != nil {
|
||||
log.Println("error creating udp socket", err)
|
||||
}
|
||||
msg := string(buf[:n])
|
||||
if !strings.HasPrefix(msg, "Q:") {
|
||||
continue
|
||||
}
|
||||
filename := strings.TrimPrefix(msg, "Q:")
|
||||
if CheckDownloaded(filename) {
|
||||
reply := fmt.Sprintf("H:%s:%d", filename, 9123)
|
||||
conn.WriteToUDP([]byte(reply), remote)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user