feat: init

This commit is contained in:
Rogee
2024-09-30 11:02:26 +08:00
parent 679759846b
commit 694dfd2a4f
90 changed files with 6046 additions and 15 deletions

37
cmd/init.go Normal file
View File

@@ -0,0 +1,37 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"os"
"dyproxy/providers/db"
"github.com/spf13/cobra"
)
// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "init db",
RunE: func(cmd *cobra.Command, args []string) error {
db, err := db.Connect("")
if err != nil {
return err
}
defer db.Close()
initSql, _ := os.ReadFile("init.sql")
_, err = db.Exec(string(initSql))
if err != nil {
return err
}
return nil
},
}
func init() {
rootCmd.AddCommand(initCmd)
}

BIN
cmd/proxy/apps.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

34
cmd/proxy/ca.crt Normal file
View File

@@ -0,0 +1,34 @@
-----BEGIN CERTIFICATE-----
MIIF9DCCA9ygAwIBAgIJAODqYUwoVjJkMA0GCSqGSIb3DQEBCwUAMIGOMQswCQYD
VQQGEwJJTDEPMA0GA1UECAwGQ2VudGVyMQwwCgYDVQQHDANMb2QxEDAOBgNVBAoM
B0dvUHJveHkxEDAOBgNVBAsMB0dvUHJveHkxGjAYBgNVBAMMEWdvcHJveHkuZ2l0
aHViLmlvMSAwHgYJKoZIhvcNAQkBFhFlbGF6YXJsQGdtYWlsLmNvbTAeFw0xNzA0
MDUyMDAwMTBaFw0zNzAzMzEyMDAwMTBaMIGOMQswCQYDVQQGEwJJTDEPMA0GA1UE
CAwGQ2VudGVyMQwwCgYDVQQHDANMb2QxEDAOBgNVBAoMB0dvUHJveHkxEDAOBgNV
BAsMB0dvUHJveHkxGjAYBgNVBAMMEWdvcHJveHkuZ2l0aHViLmlvMSAwHgYJKoZI
hvcNAQkBFhFlbGF6YXJsQGdtYWlsLmNvbTCCAiIwDQYJKoZIhvcNAQEBBQADggIP
ADCCAgoCggIBAJ4Qy+H6hhoY1s0QRcvIhxrjSHaO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LCnFGBBq5lkE7jgXME1KsbIE3lJZzRX1mQwUK8CJDYxye
i6M/dzSvy0SsPvz8fTAlprXRtWWtJQmxgWENp3Dv+0Pmux/l+ilk7KA4sMXGhsfr
bvTOeWl1/uoFTPYiWR/ww7QEPLq23yDFY04Q7Un0qjIk8ExvaY8lCkXMgc8i7sGY
VfvOYb0zm67EfAQl3TW8Ky5fl5CcxpVCD360Bzi6hwjYixa3qEeBggOixFQBFWft
8wrkKTHpOQXjn4sDPtet8imm9UYEtzWrFX6T9MFYkBR0/yye0FIh9+YPiTA6WB86
NCNwK5Yl6HuvF97CIH5CdgO+5C7KifUtqTOL8pQKbNwy0S3sNYvB+njGvRpR7pKV
BUnFpB/Atptqr4CUlTXrc5IPLAqAfmwk5IKcwy3EXUbruf9Dwz69YA==
-----END CERTIFICATE-----

BIN
cmd/proxy/icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

15
cmd/proxy/proxy.manifest Normal file
View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="SomeFunkyNameHere" type="win32"/>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
</dependentAssembly>
</dependency>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">True</dpiAware>
</windowsSettings>
</application>
</assembly>

24
cmd/proxy/proxy_linux.go Normal file
View File

@@ -0,0 +1,24 @@
package main
import (
"log"
"dyproxy/modules/proxy"
"github.com/spf13/cobra"
)
func main() {
cmd := &cobra.Command{
Use: "proxy",
Short: "serve proxy",
RunE: proxy.ServeE,
}
cmd.Flags().StringP("host", "H", "https://f.jdwan.com", "default post data to host")
cmd.Flags().BoolP("debug", "D", false, "debug mode")
cmd.Flags().IntP("duration", "d", 10, "fans fetch duration")
if err := cmd.Execute(); err != nil {
log.Fatal(err)
}
}

114
cmd/proxy/proxy_windows.go Normal file
View File

@@ -0,0 +1,114 @@
package main
import (
_ "embed"
"fmt"
"os"
"os/exec"
"strings"
"time"
"dyproxy/modules/proxy"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"github.com/sirupsen/logrus"
)
var (
outTE *walk.TextEdit
wnd *walk.MainWindow
btnService *walk.PushButton
err error
pr *proxy.Proxy
)
//go:embed ca.crt
var ca []byte
func init() {
// if go os is windows
if strings.ToLower(os.Getenv("GOOS")) == "windows" {
// get tmp path
tmpPath := os.TempDir()
// write ca.crt to tmp path
err := os.WriteFile(tmpPath+"/ca.crt", ca, 0o644)
if err != nil {
logrus.Fatal(err)
}
logrus.Info("import root cert")
// run certutil.exe -addstore root tmpPath+"/ca.crt"
cmd := exec.Command("certutil.exe", "-addstore", "root", tmpPath+"/ca.crt")
err = cmd.Run()
if err != nil {
logrus.Fatal(err)
}
}
}
func main() {
MainWindow{
AssignTo: &wnd,
Title: "粉丝代理服务",
Size: Size{300, 300},
Layout: VBox{},
Children: []Widget{
PushButton{
Text: "开启服务",
MinSize: Size{300, 50},
AssignTo: &btnService,
OnClicked: func() {
errChan := make(chan error)
if btnService.Text() == "开启服务" {
btnService.SetEnabled(false)
outTE.AppendText("服务启动中...\r\n")
if pr == nil {
pr = proxy.NewProxy("https://f.jdwan.com", false, 10)
go func() {
errChan <- pr.Serve(29999)
}()
}
select {
case err := <-errChan:
outTE.AppendText(err.Error() + "\r\n")
case <-time.After(2 * time.Second):
outTE.AppendText("服务启动成功\r\n")
btnService.SetText("停止服务")
go func() {
err = <-errChan
outTE.AppendText("[ERR] " + err.Error() + "\r\n")
btnService.SetText("开启服务")
btnService.SetEnabled(true)
}()
}
btnService.SetEnabled(true)
} else {
btnService.SetEnabled(false)
outTE.AppendText("服务停止中...\r\n")
pr.Shutdown()
outTE.AppendText("服务停止成功\r\n")
btnService.SetText("开启服务")
btnService.SetEnabled(true)
}
},
},
TextEdit{AssignTo: &outTE, ReadOnly: true},
},
}.Create()
wnd.Closing().Attach(func(canceled *bool, reason walk.CloseReason) {
fmt.Println("Application closing.")
if pr != nil {
outTE.AppendText("服务停止中...\r\n")
pr.Shutdown()
outTE.AppendText("服务停止成功\r\n")
}
})
wnd.Disposing().Attach(func() {
fmt.Println("Application has exited.")
})
wnd.Run()
}

BIN
cmd/proxy/rsrc.syso Normal file

Binary file not shown.

46
cmd/root.go Normal file
View File

@@ -0,0 +1,46 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"os"
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "dyproxy",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. 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.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
// 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/.dyproxy.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")
}

88
cmd/test.go Normal file
View File

@@ -0,0 +1,88 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"errors"
"log"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"dyproxy/.gen/model"
"dyproxy/.gen/table"
"dyproxy/providers/db"
_ "github.com/mattn/go-sqlite3"
"github.com/go-jet/jet/v2/qrm"
. "github.com/go-jet/jet/v2/sqlite"
)
// testCmd represents the test command
var testCmd = &cobra.Command{
Use: "test",
Short: "A brief description of your command",
Run: func(cmd *cobra.Command, argvs []string) {
db, err := db.Connect("")
if err != nil {
log.Fatal(err)
}
defer db.Close()
expert := model.Expert{
UID: "123",
SecUID: "123",
ShortID: "123",
RealName: "123",
NickName: "123",
}
logrus.Warnf("expert: %+v", expert)
stmt := table.Expert.SELECT(table.Expert.AllColumns).WHERE(table.Expert.UID.EQ(String(expert.UID)))
sql, args := stmt.Sql()
logrus.Debugf("sql: %s args: %+v", sql, args)
var ui model.Expert
if err := stmt.Query(db, &ui); err != nil {
if errors.Is(err, qrm.ErrNoRows) {
_, err = table.Expert.INSERT(
table.Expert.UID,
table.Expert.SecUID,
table.Expert.ShortID,
table.Expert.RealName,
table.Expert.NickName,
).VALUES(
String(expert.UID),
String(expert.SecUID),
String(expert.ShortID),
String(expert.RealName),
String(expert.NickName),
).Exec(db)
if err != nil {
log.Fatal(err)
}
log.Println("no rows")
return
}
log.Fatal("123", err)
}
log.Println("ID", ui)
},
}
func init() {
rootCmd.AddCommand(testCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// testCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// testCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

23
cmd/web/web.go Normal file
View File

@@ -0,0 +1,23 @@
package main
import (
"log"
"dyproxy/modules/web"
"github.com/spf13/cobra"
)
// webCmd represents the web command
func main() {
cmd := &cobra.Command{
Use: "web",
Short: "A brief description of your command",
RunE: web.ServeE,
}
cmd.Flags().String("config", "config.yml", "config file")
if err := cmd.Execute(); err != nil {
log.Fatal(err)
}
}