Deleting all to rewrite better
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"packets/internal"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type ConfigTOML struct {
|
||||
Config struct {
|
||||
DefaultHttpPort int `toml:"httpPort"`
|
||||
DefaultCacheDir string `toml:"cacheDir"`
|
||||
} `toml:"Config"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
internal.PacketsPackageDir()
|
||||
var cfg ConfigTOML
|
||||
toml.Decode(filepath.Join(internal.PacketsPackageDir(), "config.toml"), &cfg)
|
||||
|
||||
pid := os.Getpid()
|
||||
if err := os.WriteFile(filepath.Join(internal.PacketsPackageDir(), "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))
|
||||
}
|
||||
1872
cmd/packets/main.go
1872
cmd/packets/main.go
File diff suppressed because it is too large
Load Diff
@@ -1,65 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"packets/internal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type ConfigTOML struct {
|
||||
Config struct {
|
||||
HttpPort int `toml:"httpPort"`
|
||||
CacheDir string `toml:"cacheDir"`
|
||||
} `toml:"Config"`
|
||||
}
|
||||
|
||||
var cfg ConfigTOML
|
||||
|
||||
func CheckDownloaded(filename string) bool {
|
||||
|
||||
_, err := os.Stat(filepath.Join(cfg.Config.CacheDir))
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
pid := os.Getpid()
|
||||
if err := os.WriteFile(filepath.Join(internal.PacketsPackageDir(), "udp.pid"), []byte(fmt.Sprint(pid)), 0644); err != nil {
|
||||
fmt.Println("error saving subprocess pid", err)
|
||||
}
|
||||
toml.Decode(filepath.Join(internal.PacketsPackageDir(), "config.toml"), &cfg)
|
||||
|
||||
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, cfg.Config.HttpPort)
|
||||
conn.WriteToUDP([]byte(reply), remote)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user