Prepare listening servers

This commit is contained in:
raul 2024-06-03 09:24:18 +02:00
parent 45b6fcacdd
commit e8b29d5baa
5 changed files with 113 additions and 20 deletions

27
cmd/httpServer.go Normal file
View File

@ -0,0 +1,27 @@
package cmd
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
var (
WebPort string = "8080"
)
func WebServer() {
p := viper.GetString("Server.WebPort")
if p != "" {
WebPort = p
}
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.GET("/", getRoot)
r.Run(":" + WebPort)
}
func getRoot(c *gin.Context) {
c.String(http.StatusOK, "Hello world!\n")
}

View File

@ -1,11 +1,10 @@
/*
Copyright © 2024 raul
Copyright © 2024 raul <raul@bulgariu.xyz>
*/
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
@ -40,16 +39,7 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.tiamat.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.config/tiamat/tiamat.toml)")
}
// initConfig reads in config file and ENV variables if set.
@ -62,16 +52,15 @@ func initConfig() {
home, err := os.UserHomeDir()
cobra.CheckErr(err)
// Search config in home directory with name ".tiamat" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".tiamat")
// Search config in home directory with name ".tune" (without extension).
viper.AddConfigPath(home + "/.config/tiamat")
viper.SetConfigType("toml")
viper.SetConfigName("tiamat")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}

38
cmd/server.go Normal file
View File

@ -0,0 +1,38 @@
/*
Copyright © 2024 raul <raul@bulgariu.xyz
*/
package cmd
import (
"github.com/spf13/cobra"
)
// serverCmd represents the server command
var serverCmd = &cobra.Command{
Use: "server",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
Server()
},
}
func init() {
rootCmd.AddCommand(serverCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serverCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

39
cmd/serverFunc.go Normal file
View File

@ -0,0 +1,39 @@
package cmd
import (
"log"
"net"
"github.com/spf13/viper"
)
var (
C2Port string = "1337"
)
func Server() {
p := viper.GetString("Server.Port")
if p != "" {
C2Port = p
}
go WebServer()
ln, err := net.Listen("tcp", ":"+C2Port)
if err != nil {
log.Fatalf("Error happened listening on C2 port: %v\n", err)
}
log.Printf("Listening on port %v...", C2Port)
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("Error happened accepting connection: %v\n", err)
}
handleConn(conn)
}
}
func handleConn(conn net.Conn) {
}

View File

@ -1,7 +1,7 @@
/*
Copyright © 2024 raul
Copyright © 2024 raul <raul@bulgariu.xyz>
*/
package main
import "tiamat/cmd"