Implement UDP socket server and add package installation check utility
This commit is contained in:
		
							
								
								
									
										36
									
								
								cmd/httpsocket/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								cmd/httpsocket/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"packets/configs"
 | 
			
		||||
	"packets/internal/consts"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type ConfigTOML struct {
 | 
			
		||||
	Config struct {
 | 
			
		||||
		DefaultHttpPort int    `toml:"httpPort"`
 | 
			
		||||
		DefaultCacheDir string `toml:"cacheDir"`
 | 
			
		||||
	} `toml:"Config"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
 | 
			
		||||
	cfg, err := configs.GetConfigTOML()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pid := os.Getpid()
 | 
			
		||||
	if err := os.WriteFile(filepath.Join(consts.DefaultLinux_d, "http.pid"), []byte(fmt.Sprint(pid)), 0664); err != nil {
 | 
			
		||||
		fmt.Println("error saving subprocess pid", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fs := http.FileServer(http.Dir(cfg.Config.Cache_d))
 | 
			
		||||
	http.Handle("/", fs)
 | 
			
		||||
 | 
			
		||||
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", cfg.Config.HttpPort), nil))
 | 
			
		||||
}
 | 
			
		||||
@@ -30,7 +30,7 @@ func init() {
 | 
			
		||||
 | 
			
		||||
	_, err := os.Stat(consts.DefaultLinux_d)
 | 
			
		||||
	if os.IsNotExist(err) {
 | 
			
		||||
		err := os.Mkdir(consts.DefaultLinux_d, 0755)
 | 
			
		||||
		err := os.Mkdir(consts.DefaultLinux_d, 0777)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			if os.IsPermission(err) {
 | 
			
		||||
				log.Fatal("can't create packets root directory, please run as root")
 | 
			
		||||
@@ -184,6 +184,14 @@ var installCmd = &cobra.Command{
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			if exist {
 | 
			
		||||
				installed, err := utils.CheckIfPackageInstalled(inputName)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Fatal(err)
 | 
			
		||||
				}
 | 
			
		||||
				if installed {
 | 
			
		||||
					fmt.Printf(":: Package %s is already installed\n", inputName)
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
				fmt.Printf(":: Downloading (%s) \n", inputName)
 | 
			
		||||
				p, err := packets.GetPackage(inputName)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
@@ -236,13 +244,23 @@ var installCmd = &cobra.Command{
 | 
			
		||||
			}
 | 
			
		||||
			switch len(pkgs) {
 | 
			
		||||
			case 0:
 | 
			
		||||
				log.Fatalf("can't find any results for %s\n", inputName)
 | 
			
		||||
				log.Fatalf("can't find any results for (%s)\n", inputName)
 | 
			
		||||
 | 
			
		||||
			case 1:
 | 
			
		||||
				fmt.Printf(":: Founded 1 package for %s \n", inputName)
 | 
			
		||||
 | 
			
		||||
				installed, err := utils.CheckIfPackageInstalled(pkgs[0].Name)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Fatal(err)
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				if installed {
 | 
			
		||||
					fmt.Printf(":: Package %s is already installed\n", pkgs[0].Name)
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				fmt.Printf(":: Downloading %s \n", pkgs[0].Name)
 | 
			
		||||
				p, err := packets.GetPackage(inputName)
 | 
			
		||||
				p, err := packets.GetPackage(pkgs[0].Name)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Fatal(err)
 | 
			
		||||
				}
 | 
			
		||||
@@ -253,7 +271,7 @@ var installCmd = &cobra.Command{
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				reader := bytes.NewReader(p.PackageF)
 | 
			
		||||
				fmt.Printf(":: Installing (%s) \n", pkgs[0].Name)
 | 
			
		||||
				fmt.Printf(":: Installing %s \n", pkgs[0].Name)
 | 
			
		||||
				packets.InstallPackage(reader)
 | 
			
		||||
 | 
			
		||||
				if cfg.Config.StorePackages {
 | 
			
		||||
@@ -288,6 +306,15 @@ var installCmd = &cobra.Command{
 | 
			
		||||
					goto optionagain
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				installed, err := utils.CheckIfPackageInstalled(pkgs[choice].Name)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Fatal(err)
 | 
			
		||||
				}
 | 
			
		||||
				if installed {
 | 
			
		||||
					fmt.Printf(":: Package %s is already installed\n", pkgs[choice].Name)
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				fmt.Printf(":: Downloading %s \n", pkgs[choice].Name)
 | 
			
		||||
				p, err := packets.GetPackage(pkgs[choice].Name)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										58
									
								
								cmd/udpsocket/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								cmd/udpsocket/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,58 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net"
 | 
			
		||||
	"os"
 | 
			
		||||
	"packets/configs"
 | 
			
		||||
	"packets/internal/consts"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func CheckDownloaded(filename string) bool {
 | 
			
		||||
 | 
			
		||||
	cfg, err := configs.GetConfigTOML()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = os.Stat(filepath.Join(cfg.Config.Cache_d, filename))
 | 
			
		||||
	return err == nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	pid := os.Getpid()
 | 
			
		||||
	if err := os.WriteFile(filepath.Join(consts.DefaultLinux_d, "udp.pid"), []byte(fmt.Sprint(pid)), 0664); err != nil {
 | 
			
		||||
		fmt.Println("error saving subprocess pid", err)
 | 
			
		||||
	}
 | 
			
		||||
	cfg, err := configs.GetConfigTOML()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal(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, cfg.Config.HttpPort)
 | 
			
		||||
			conn.WriteToUDP([]byte(reply), remote)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -239,3 +239,19 @@ func (p *Package) AddToInstalledDB(inCache int, packagePath string) error {
 | 
			
		||||
	success = true
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func CheckIfPackageInstalled(name string) (bool, error) {
 | 
			
		||||
	db, err := sql.Open("sqlite", consts.InstalledDB)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return false, err
 | 
			
		||||
	}
 | 
			
		||||
	defer db.Close()
 | 
			
		||||
 | 
			
		||||
	var exists bool
 | 
			
		||||
	err = db.QueryRow("SELECT EXISTS(SELECT 1 FROM packages WHERE name = ?)", name).Scan(&exists)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return false, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return exists, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user