forked from fossabot/clash
-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edbb135
commit 0b93d1b
Showing
7 changed files
with
268 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"path/filepath" | ||
"syscall" | ||
|
||
"github.com/doreamon-design/clash" | ||
"github.com/doreamon-design/clash/config" | ||
"github.com/doreamon-design/clash/hub" | ||
"github.com/doreamon-design/clash/hub/executor" | ||
"github.com/doreamon-design/clash/log" | ||
"github.com/go-zoox/cli" | ||
"go.uber.org/automaxprocs/maxprocs" | ||
|
||
C "github.com/doreamon-design/clash/constant" | ||
) | ||
|
||
func main() { | ||
app := cli.NewSingleProgram(&cli.SingleProgramConfig{ | ||
Name: "clash", | ||
Usage: "A rule-based tunnel in Go", | ||
Version: clash.Version, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "home-dir", | ||
Usage: "set configuration directory", | ||
EnvVars: []string{"CLASH_HOME_DIR"}, | ||
Aliases: []string{"d"}, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "config", | ||
Usage: "specify configuration file", | ||
EnvVars: []string{"CLASH_CONFIG_FILE"}, | ||
Aliases: []string{"f"}, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "ext-ui", | ||
Usage: "override external ui directory", | ||
EnvVars: []string{"CLASH_OVERRIDE_EXTERNAL_UI_DIR"}, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "ext-ctl", | ||
Usage: "override external controller address", | ||
EnvVars: []string{"CLASH_OVERRIDE_EXTERNAL_CONTROLLER"}, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "secret", | ||
Usage: "override secret for RESTful API", | ||
EnvVars: []string{"CLASH_OVERRIDE_SECRET"}, | ||
}, | ||
&cli.BoolFlag{ | ||
Name: "test", | ||
Usage: "test configuration and exit", | ||
Aliases: []string{"t"}, | ||
}, | ||
}, | ||
}) | ||
|
||
app.Command(func(ctx *cli.Context) error { | ||
homeDir := ctx.String("home-dir") | ||
configFile := ctx.String("config") | ||
externalUI := ctx.String("ext-ui") | ||
externalController := ctx.String("ext-ctl") | ||
secret := ctx.String("secret") | ||
testConfig := ctx.Bool("test") | ||
|
||
maxprocs.Set(maxprocs.Logger(func(string, ...any) {})) | ||
|
||
if homeDir != "" { | ||
if !filepath.IsAbs(homeDir) { | ||
currentDir, _ := os.Getwd() | ||
homeDir = filepath.Join(currentDir, homeDir) | ||
} | ||
C.SetHomeDir(homeDir) | ||
} | ||
|
||
if configFile != "" { | ||
if !filepath.IsAbs(configFile) { | ||
currentDir, _ := os.Getwd() | ||
configFile = filepath.Join(currentDir, configFile) | ||
} | ||
C.SetConfig(configFile) | ||
} else { | ||
configFile := filepath.Join(C.Path.HomeDir(), C.Path.Config()) | ||
C.SetConfig(configFile) | ||
} | ||
|
||
if err := config.Init(C.Path.HomeDir()); err != nil { | ||
log.Fatalln("Initial configuration directory error: %s", err.Error()) | ||
} | ||
|
||
if testConfig { | ||
if _, err := executor.Parse(); err != nil { | ||
log.Errorln(err.Error()) | ||
fmt.Printf("configuration file %s test failed\n", C.Path.Config()) | ||
os.Exit(1) | ||
} | ||
fmt.Printf("configuration file %s test is successful\n", C.Path.Config()) | ||
return nil | ||
} | ||
|
||
var options []hub.Option | ||
if externalUI != "" { | ||
options = append(options, hub.WithExternalUI(externalUI)) | ||
} | ||
if externalController != "" { | ||
options = append(options, hub.WithExternalController(externalController)) | ||
} | ||
if secret != "" { | ||
options = append(options, hub.WithSecret(secret)) | ||
} | ||
|
||
if err := hub.Parse(options...); err != nil { | ||
log.Fatalln("Parse config error: %s", err.Error()) | ||
} | ||
|
||
termSign := make(chan os.Signal, 1) | ||
hupSign := make(chan os.Signal, 1) | ||
signal.Notify(termSign, syscall.SIGINT, syscall.SIGTERM) | ||
signal.Notify(hupSign, syscall.SIGHUP) | ||
for { | ||
select { | ||
case <-termSign: | ||
return nil | ||
case <-hupSign: | ||
if cfg, err := executor.ParseWithPath(C.Path.Config()); err == nil { | ||
executor.ApplyConfig(cfg, true) | ||
} else { | ||
log.Errorln("Parse config error: %s", err.Error()) | ||
} | ||
} | ||
} | ||
}) | ||
|
||
app.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.