mirror of https://github.com/dnomd343/XProxy.git
dnomd343
2 years ago
8 changed files with 246 additions and 224 deletions
@ -1,94 +1,89 @@ |
|||
package common |
|||
|
|||
import ( |
|||
log "github.com/sirupsen/logrus" |
|||
"io" |
|||
"io/ioutil" |
|||
"net/http" |
|||
"os" |
|||
"strings" |
|||
log "github.com/sirupsen/logrus" |
|||
"io" |
|||
"io/ioutil" |
|||
"net/http" |
|||
"os" |
|||
"strings" |
|||
) |
|||
|
|||
func CreateFolder(folderPath string) { |
|||
log.Debugf("Create folder -> %s", folderPath) |
|||
err := os.MkdirAll(folderPath, 0755) |
|||
if err != nil { |
|||
log.Panicf("Failed to create folder -> %s", folderPath) |
|||
} |
|||
log.Debugf("Create folder -> %s", folderPath) |
|||
if err := os.MkdirAll(folderPath, 0755); err != nil { |
|||
log.Panicf("Failed to create folder -> %s", folderPath) |
|||
} |
|||
} |
|||
|
|||
func IsFileExist(filePath string) bool { |
|||
s, err := os.Stat(filePath) |
|||
if err != nil { // file or folder not exist
|
|||
return false |
|||
} |
|||
return !s.IsDir() |
|||
s, err := os.Stat(filePath) |
|||
if err != nil { // file or folder not exist
|
|||
return false |
|||
} |
|||
return !s.IsDir() |
|||
} |
|||
|
|||
func WriteFile(filePath string, content string, overwrite bool) { |
|||
if !overwrite && IsFileExist(filePath) { // file exist and don't overwrite
|
|||
log.Debugf("File `%s` exist -> skip write", filePath) |
|||
return |
|||
} |
|||
log.Debugf("Write file `%s` -> \n%s", filePath, content) |
|||
err := os.WriteFile(filePath, []byte(content), 0644) |
|||
if err != nil { |
|||
log.Panicf("Failed to write `%s` -> %v", filePath, err) |
|||
} |
|||
if !overwrite && IsFileExist(filePath) { // file exist and don't overwrite
|
|||
log.Debugf("File `%s` exist -> skip write", filePath) |
|||
return |
|||
} |
|||
log.Debugf("Write file `%s` -> \n%s", filePath, content) |
|||
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil { |
|||
log.Panicf("Failed to write `%s` -> %v", filePath, err) |
|||
} |
|||
} |
|||
|
|||
func ListFiles(folderPath string, suffix string) []string { |
|||
var fileList []string |
|||
files, err := ioutil.ReadDir(folderPath) |
|||
if err != nil { |
|||
log.Panicf("Failed to list folder -> %s", folderPath) |
|||
} |
|||
for _, file := range files { |
|||
if strings.HasSuffix(file.Name(), suffix) { |
|||
fileList = append(fileList, file.Name()) |
|||
} |
|||
} |
|||
return fileList |
|||
var fileList []string |
|||
files, err := ioutil.ReadDir(folderPath) |
|||
if err != nil { |
|||
log.Panicf("Failed to list folder -> %s", folderPath) |
|||
} |
|||
for _, file := range files { |
|||
if strings.HasSuffix(file.Name(), suffix) { |
|||
fileList = append(fileList, file.Name()) |
|||
} |
|||
} |
|||
return fileList |
|||
} |
|||
|
|||
func CopyFile(source string, target string) { |
|||
log.Infof("Copy file `%s` => `%s`", source, target) |
|||
if IsFileExist(target) { |
|||
log.Warningf("File `%s` will be overrided", target) |
|||
} |
|||
srcFile, err := os.Open(source) |
|||
if err != nil { |
|||
log.Panicf("Failed to open file -> %s", source) |
|||
} |
|||
dstFile, err := os.OpenFile(target, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) |
|||
if err != nil { |
|||
_ = srcFile.Close() |
|||
log.Panicf("Failed to open file -> %s", target) |
|||
} |
|||
_, err = io.Copy(dstFile, srcFile) |
|||
_ = srcFile.Close() |
|||
_ = dstFile.Close() |
|||
if err != nil { |
|||
log.Panicf("Failed to copy from `%s` to `%s`", source, target) |
|||
} |
|||
log.Infof("Copy file `%s` => `%s`", source, target) |
|||
if IsFileExist(target) { |
|||
log.Debugf("File `%s` will be overrided", target) |
|||
} |
|||
srcFile, err := os.Open(source) |
|||
defer srcFile.Close() |
|||
if err != nil { |
|||
log.Panicf("Failed to open file -> %s", source) |
|||
} |
|||
dstFile, err := os.OpenFile(target, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) |
|||
defer dstFile.Close() |
|||
if err != nil { |
|||
log.Panicf("Failed to open file -> %s", target) |
|||
} |
|||
if _, err = io.Copy(dstFile, srcFile); err != nil { |
|||
log.Panicf("Failed to copy from `%s` to `%s`", source, target) |
|||
} |
|||
} |
|||
|
|||
func DownloadFile(url string, file string) { |
|||
log.Debugf("File download `%s` => `%s`", url, file) |
|||
resp, err := http.Get(url) |
|||
if err != nil { |
|||
log.Errorf("Download `%s` error -> %v", url, err) |
|||
return |
|||
} |
|||
out, err := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) |
|||
if err != nil { |
|||
_ = resp.Body.Close() |
|||
log.Panicf("Open `%s` error -> %v", file, err) |
|||
} |
|||
_, err = io.Copy(out, resp.Body) |
|||
_ = resp.Body.Close() |
|||
if err != nil { |
|||
log.Panicf("File `%s` save error -> %v", file, err) |
|||
} |
|||
log.Infof("Download success `%s` => `%s`", url, file) |
|||
log.Debugf("File download `%s` => `%s`", url, file) |
|||
resp, err := http.Get(url) |
|||
defer resp.Body.Close() |
|||
if err != nil { |
|||
log.Errorf("Download `%s` error -> %v", url, err) |
|||
return |
|||
} |
|||
output, err := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) |
|||
defer output.Close() |
|||
if err != nil { |
|||
log.Panicf("Open `%s` error -> %v", file, err) |
|||
} |
|||
if _, err = io.Copy(output, resp.Body); err != nil { |
|||
log.Panicf("File `%s` save error -> %v", file, err) |
|||
} |
|||
log.Infof("Download success `%s` => `%s`", url, file) |
|||
} |
|||
|
@ -0,0 +1,43 @@ |
|||
package common |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
log "github.com/sirupsen/logrus" |
|||
"net" |
|||
"os/exec" |
|||
"strings" |
|||
"syscall" |
|||
) |
|||
|
|||
func isIP(ipAddr string, isCidr bool) bool { |
|||
if !isCidr { |
|||
return net.ParseIP(ipAddr) != nil |
|||
} |
|||
_, _, err := net.ParseCIDR(ipAddr) |
|||
return err == nil |
|||
} |
|||
|
|||
func IsIPv4(ipAddr string, isCidr bool) bool { |
|||
return isIP(ipAddr, isCidr) && strings.Contains(ipAddr, ".") |
|||
} |
|||
|
|||
func IsIPv6(ipAddr string, isCidr bool) bool { |
|||
return isIP(ipAddr, isCidr) && strings.Contains(ipAddr, ":") |
|||
} |
|||
|
|||
func JsonEncode(raw interface{}) string { |
|||
jsonOutput, _ := json.MarshalIndent(raw, "", " ") // json encode
|
|||
return string(jsonOutput) |
|||
} |
|||
|
|||
func RunCommand(command ...string) (int, string) { |
|||
log.Debugf("Running system command -> %v", command) |
|||
process := exec.Command(command[0], command[1:]...) |
|||
output, _ := process.CombinedOutput() |
|||
log.Debugf("Command %v -> \n%s", command, string(output)) |
|||
code := process.ProcessState.Sys().(syscall.WaitStatus).ExitStatus() |
|||
if code != 0 { |
|||
log.Warningf("Command %v return code %d", command, code) |
|||
} |
|||
return code, string(output) |
|||
} |
@ -1,22 +0,0 @@ |
|||
package common |
|||
|
|||
import ( |
|||
"net" |
|||
"strings" |
|||
) |
|||
|
|||
func isIP(ipAddr string, isCidr bool) bool { |
|||
if !isCidr { |
|||
return net.ParseIP(ipAddr) != nil |
|||
} |
|||
_, _, err := net.ParseCIDR(ipAddr) |
|||
return err == nil |
|||
} |
|||
|
|||
func IsIPv4(ipAddr string, isCidr bool) bool { |
|||
return isIP(ipAddr, isCidr) && strings.Contains(ipAddr, ".") |
|||
} |
|||
|
|||
func IsIPv6(ipAddr string, isCidr bool) bool { |
|||
return isIP(ipAddr, isCidr) && strings.Contains(ipAddr, ":") |
|||
} |
@ -1,19 +0,0 @@ |
|||
package common |
|||
|
|||
import ( |
|||
log "github.com/sirupsen/logrus" |
|||
"os/exec" |
|||
"syscall" |
|||
) |
|||
|
|||
func RunCommand(command ...string) (int, string) { |
|||
log.Debugf("Running system command -> %v", command) |
|||
process := exec.Command(command[0], command[1:]...) |
|||
output, _ := process.CombinedOutput() |
|||
log.Debugf("Command %v -> \n%s", command, string(output)) |
|||
code := process.ProcessState.Sys().(syscall.WaitStatus).ExitStatus() |
|||
if code != 0 { |
|||
log.Warningf("Command %v return code %d", command, code) |
|||
} |
|||
return code, string(output) |
|||
} |
@ -1,37 +0,0 @@ |
|||
package proxy |
|||
|
|||
type Config struct { |
|||
Sniff bool |
|||
Redirect bool |
|||
V4TProxyPort int |
|||
V6TProxyPort int |
|||
LogLevel string |
|||
HttpInbounds map[string]int |
|||
SocksInbounds map[string]int |
|||
AddOnInbounds []interface{} |
|||
} |
|||
|
|||
type logObject struct { |
|||
Loglevel string `json:"loglevel"` |
|||
Access string `json:"access"` |
|||
Error string `json:"error"` |
|||
} |
|||
|
|||
type inboundsObject struct { |
|||
Inbounds []interface{} `json:"inbounds"` |
|||
} |
|||
|
|||
type sniffObject struct { |
|||
Enabled bool `json:"enabled"` |
|||
RouteOnly bool `json:"routeOnly"` |
|||
DestOverride []string `json:"destOverride"` |
|||
} |
|||
|
|||
type inboundObject struct { |
|||
Tag string `json:"tag"` |
|||
Port int `json:"port"` |
|||
Protocol string `json:"protocol"` |
|||
Settings interface{} `json:"settings"` |
|||
StreamSettings interface{} `json:"streamSettings"` |
|||
Sniffing sniffObject `json:"sniffing"` |
|||
} |
Loading…
Reference in new issue