-
Notifications
You must be signed in to change notification settings - Fork 60
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
x
committed
Nov 10, 2024
1 parent
a3c205c
commit e895661
Showing
29 changed files
with
1,060 additions
and
662 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
"github.com/hiddify/hiddify-core/v2/hcommon" | ||
hcore "github.com/hiddify/hiddify-core/v2/hcore" | ||
|
||
"github.com/spf13/cobra" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
var execCommand = &cobra.Command{ | ||
Use: "command", | ||
Short: "command", | ||
Args: cobra.OnlyValidArgs, | ||
Run: execCommandgrpc, | ||
} | ||
|
||
func init() { | ||
// commandRun.PersistentFlags().BoolP("help", "", false, "help for this command") | ||
// commandRun.Flags().StringVarP(&hiddifySettingPath, "hiddify", "d", "", "Hiddify Setting JSON Path") | ||
|
||
mainCommand.AddCommand(execCommand) | ||
} | ||
|
||
func execCommandgrpc(cmd *cobra.Command, args []string) { | ||
conn, err := grpc.Dial("127.0.0.1:17078", grpc.WithInsecure()) | ||
if err != nil { | ||
log.Fatalf("did not connect: %v", err) | ||
} | ||
defer conn.Close() | ||
c := hcore.NewCoreClient(conn) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*100) | ||
defer cancel() | ||
tream, err := c.CoreInfoListener(ctx, &hcommon.Empty{}) | ||
stream2, err := c.OutboundsInfo(ctx, &hcommon.Empty{}) | ||
stream, err := c.MainOutboundsInfo(ctx, &hcommon.Empty{}) | ||
logstream, err := c.LogListener(ctx, &hcommon.Empty{}) | ||
if err != nil { | ||
log.Fatalf("could not stream: %v", err) | ||
} | ||
|
||
go coreLogger(tream, "coreinfo") | ||
go coreLogger(stream2, "alloutbounds") | ||
go coreLogger(stream, "mainoutbounds") | ||
go coreLogger(logstream, "log") | ||
for _, x := range []string{ | ||
"m4 § 0", | ||
"warp in warp § 1", | ||
"LocalIP § 2", | ||
"WarpInWarp✅ § 3", | ||
} { | ||
c.UrlTest(ctx, &hcore.UrlTestRequest{GroupTag: "auto"}) | ||
log.Printf("Sending: %s", x) | ||
resp, err := c.SelectOutbound(ctx, &hcore.SelectOutboundRequest{ | ||
GroupTag: "select", | ||
OutboundTag: x, | ||
}) | ||
if err != nil { | ||
log.Fatalf("could not greet: %s %v", x, err) | ||
} | ||
|
||
log.Printf("Received: %s %v", x, resp) | ||
<-time.After(1 * time.Second) | ||
} | ||
<-time.After(10 * time.Second) | ||
} | ||
|
||
func coreLogger[T any](stream grpc.ServerStreamingClient[T], name string) { | ||
for { | ||
c, err := stream.Recv() | ||
if err != nil { | ||
log.Printf("could not receive %s : %v", name, err) | ||
} | ||
log.Printf("Received: %s, %v", name, c) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,45 +1,34 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/hiddify/hiddify-core/v2/hcommon" | ||
hcore "github.com/hiddify/hiddify-core/v2/hcore" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
const ( | ||
address = "localhost:17078" | ||
defaultName = "world" | ||
) | ||
|
||
func init() { | ||
if os.Args[1] == "test" { | ||
conn, err := grpc.Dial(address, grpc.WithInsecure()) | ||
if err != nil { | ||
log.Fatalf("did not connect: %v", err) | ||
} | ||
defer conn.Close() | ||
c := hcore.NewCoreClient(conn) | ||
|
||
// ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) | ||
// defer cancel() | ||
ctx := context.Background() | ||
|
||
// SayHello | ||
stream, err := c.OutboundsInfo(ctx, &hcommon.Empty{}) | ||
|
||
for { | ||
r, err := stream.Recv() | ||
if err != nil { | ||
log.Fatalf("could not receive: %v", err) | ||
} | ||
log.Printf("Received1: %s", r.String()) | ||
|
||
time.Sleep(1 * time.Second) | ||
} | ||
} | ||
} | ||
// const ( | ||
// address = "localhost:17078" | ||
// defaultName = "world" | ||
// ) | ||
|
||
// // func init() { | ||
// // if len(os.Args) > 1 && os.Args[1] == "test" { | ||
// // conn, err := grpc.Dial(address, grpc.WithInsecure()) | ||
// // if err != nil { | ||
// // log.Fatalf("did not connect: %v", err) | ||
// // } | ||
// // defer conn.Close() | ||
// // c := hcore.NewCoreClient(conn) | ||
|
||
// // ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) | ||
// // defer cancel() | ||
// ctx := context.Background() | ||
|
||
// // SayHello | ||
// stream, err := c.OutboundsInfo(ctx, &hcommon.Empty{}) | ||
|
||
// for { | ||
// r, err := stream.Recv() | ||
// if err != nil { | ||
// log.Fatalf("could not receive: %v", err) | ||
// } | ||
// log.Printf("Received1: %s", r.String()) | ||
|
||
// time.Sleep(1 * time.Second) | ||
// } | ||
// } | ||
// } |
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
Oops, something went wrong.