mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 06:08:34 +00:00
Add openvpn and openconnect
This commit is contained in:
@@ -879,32 +879,26 @@ func (c *CommandClient) StartSTUNTest(server string, outboundTag string, handler
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (c *CommandClient) SubscribeTailscaleStatus(handler TailscaleStatusHandler) (*TailscaleStatusSubscription, error) {
|
||||
func subscribeStatus[T any](c *CommandClient, session *streamSession, name string, start func(context.Context, daemon.StartedServiceClient) (grpc.ServerStreamingClient[T], error), onUpdate func(*T), onError func(string)) error {
|
||||
client, parentCtx, err := c.getClientForCall()
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "subscribe tailscale status")
|
||||
return E.Cause(err, "subscribe ", name)
|
||||
}
|
||||
|
||||
streamCtx, cancel := context.WithCancel(parentCtx)
|
||||
session := &TailscaleStatusSubscription{
|
||||
streamSession: streamSession{
|
||||
ctx: streamCtx,
|
||||
cancel: cancel,
|
||||
closeDone: make(chan struct{}),
|
||||
},
|
||||
*session = streamSession{
|
||||
ctx: streamCtx,
|
||||
cancel: cancel,
|
||||
closeDone: make(chan struct{}),
|
||||
}
|
||||
|
||||
failStart := func(cause error, message string) (*TailscaleStatusSubscription, error) {
|
||||
stream, err := start(streamCtx, client)
|
||||
if err != nil {
|
||||
cancel()
|
||||
if c.standalone {
|
||||
c.closeConnection()
|
||||
}
|
||||
return nil, E.Cause(cause, message)
|
||||
}
|
||||
|
||||
stream, err := client.SubscribeTailscaleStatus(streamCtx, &emptypb.Empty{})
|
||||
if err != nil {
|
||||
return failStart(err, "subscribe tailscale status")
|
||||
return E.Cause(err, "subscribe ", name)
|
||||
}
|
||||
|
||||
standalone := c.standalone
|
||||
@@ -924,71 +918,123 @@ func (c *CommandClient) SubscribeTailscaleStatus(handler TailscaleStatusHandler)
|
||||
if status.Code(recvErr) == codes.NotFound || status.Code(recvErr) == codes.Unavailable {
|
||||
return
|
||||
}
|
||||
handler.OnError(E.Cause(recvErr, "tailscale status recv").Error())
|
||||
onError(E.Cause(recvErr, name, " recv").Error())
|
||||
return
|
||||
}
|
||||
handler.OnStatusUpdate(tailscaleStatusUpdateFromGRPC(event))
|
||||
onUpdate(event)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *CommandClient) SubscribeTailscaleStatus(handler TailscaleStatusHandler) (*TailscaleStatusSubscription, error) {
|
||||
session := new(TailscaleStatusSubscription)
|
||||
err := subscribeStatus(c, &session.streamSession, "tailscale status", func(ctx context.Context, client daemon.StartedServiceClient) (grpc.ServerStreamingClient[daemon.TailscaleStatusUpdate], error) {
|
||||
return client.SubscribeTailscaleStatus(ctx, &emptypb.Empty{})
|
||||
}, func(update *daemon.TailscaleStatusUpdate) {
|
||||
handler.OnStatusUpdate(tailscaleStatusUpdateFromGRPC(update))
|
||||
}, handler.OnError)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (c *CommandClient) SubscribeUSBIPServerStatus(handler USBIPServerStatusHandler) (*USBIPServerStatusSubscription, error) {
|
||||
client, parentCtx, err := c.getClientForCall()
|
||||
session := new(USBIPServerStatusSubscription)
|
||||
err := subscribeStatus(c, &session.streamSession, "usbip server status", func(ctx context.Context, client daemon.StartedServiceClient) (grpc.ServerStreamingClient[daemon.USBIPServerStatusUpdate], error) {
|
||||
return client.SubscribeUSBIPServerStatus(ctx, &emptypb.Empty{})
|
||||
}, func(update *daemon.USBIPServerStatusUpdate) {
|
||||
handler.OnStatusUpdate(usbipServerStatusUpdateFromGRPC(update))
|
||||
}, handler.OnError)
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "subscribe usbip server status")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
streamCtx, cancel := context.WithCancel(parentCtx)
|
||||
session := &USBIPServerStatusSubscription{
|
||||
streamSession: streamSession{
|
||||
ctx: streamCtx,
|
||||
cancel: cancel,
|
||||
closeDone: make(chan struct{}),
|
||||
},
|
||||
}
|
||||
|
||||
failStart := func(cause error, message string) (*USBIPServerStatusSubscription, error) {
|
||||
cancel()
|
||||
if c.standalone {
|
||||
c.closeConnection()
|
||||
}
|
||||
return nil, E.Cause(cause, message)
|
||||
}
|
||||
|
||||
stream, err := client.SubscribeUSBIPServerStatus(streamCtx, &emptypb.Empty{})
|
||||
if err != nil {
|
||||
return failStart(err, "subscribe usbip server status")
|
||||
}
|
||||
|
||||
standalone := c.standalone
|
||||
go func() {
|
||||
defer func() {
|
||||
close(session.closeDone)
|
||||
if standalone {
|
||||
c.closeConnection()
|
||||
}
|
||||
}()
|
||||
for {
|
||||
event, recvErr := stream.Recv()
|
||||
if recvErr != nil {
|
||||
if session.ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
if status.Code(recvErr) == codes.NotFound || status.Code(recvErr) == codes.Unavailable {
|
||||
return
|
||||
}
|
||||
handler.OnError(E.Cause(recvErr, "usbip server status recv").Error())
|
||||
return
|
||||
}
|
||||
handler.OnStatusUpdate(usbipServerStatusUpdateFromGRPC(event))
|
||||
}
|
||||
}()
|
||||
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (c *CommandClient) SubscribeOpenConnectStatus(handler OpenConnectStatusHandler) (*OpenConnectStatusSubscription, error) {
|
||||
session := new(OpenConnectStatusSubscription)
|
||||
err := subscribeStatus(c, &session.streamSession, "openconnect status", func(ctx context.Context, client daemon.StartedServiceClient) (grpc.ServerStreamingClient[daemon.OpenConnectStatusUpdate], error) {
|
||||
return client.SubscribeOpenConnectStatus(ctx, &emptypb.Empty{})
|
||||
}, func(update *daemon.OpenConnectStatusUpdate) {
|
||||
handler.OnStatusUpdate(openConnectStatusUpdateFromGRPC(update))
|
||||
}, handler.OnError)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (c *CommandClient) SubmitOpenConnectAuthForm(endpointTag string, formID string, values *OpenConnectFormValues) error {
|
||||
_, err := callWithResult(c, func(ctx context.Context, client daemon.StartedServiceClient) (*emptypb.Empty, error) {
|
||||
return client.SubmitOpenConnectAuthForm(ctx, &daemon.OpenConnectAuthFormSubmission{
|
||||
EndpointTag: endpointTag,
|
||||
FormID: formID,
|
||||
Values: values.values,
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return E.Cause(err, "submit openconnect authentication form")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *CommandClient) CancelOpenConnectAuthForm(endpointTag string, formID string) error {
|
||||
_, err := callWithResult(c, func(ctx context.Context, client daemon.StartedServiceClient) (*emptypb.Empty, error) {
|
||||
return client.CancelOpenConnectAuthForm(ctx, &daemon.OpenConnectAuthFormCancel{
|
||||
EndpointTag: endpointTag,
|
||||
FormID: formID,
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return E.Cause(err, "cancel openconnect authentication form")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *CommandClient) SubscribeOpenVPNStatus(handler OpenVPNStatusHandler) (*OpenVPNStatusSubscription, error) {
|
||||
session := new(OpenVPNStatusSubscription)
|
||||
err := subscribeStatus(c, &session.streamSession, "openvpn status", func(ctx context.Context, client daemon.StartedServiceClient) (grpc.ServerStreamingClient[daemon.OpenVPNStatusUpdate], error) {
|
||||
return client.SubscribeOpenVPNStatus(ctx, &emptypb.Empty{})
|
||||
}, func(update *daemon.OpenVPNStatusUpdate) {
|
||||
handler.OnStatusUpdate(openVPNStatusUpdateFromGRPC(update))
|
||||
}, handler.OnError)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (c *CommandClient) SubmitOpenVPNChallengeResponse(endpointTag string, challengeID string, response *OpenVPNChallengeResponse) error {
|
||||
_, err := callWithResult(c, func(ctx context.Context, client daemon.StartedServiceClient) (*emptypb.Empty, error) {
|
||||
return client.SubmitOpenVPNChallengeResponse(ctx, &daemon.OpenVPNChallengeSubmission{
|
||||
EndpointTag: endpointTag,
|
||||
ChallengeID: challengeID,
|
||||
Username: response.Username,
|
||||
Password: response.Password,
|
||||
Secret: response.Secret,
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return E.Cause(err, "submit openvpn challenge response")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *CommandClient) CancelOpenVPNChallenge(endpointTag string, challengeID string) error {
|
||||
_, err := callWithResult(c, func(ctx context.Context, client daemon.StartedServiceClient) (*emptypb.Empty, error) {
|
||||
return client.CancelOpenVPNChallenge(ctx, &daemon.OpenVPNChallengeCancel{
|
||||
EndpointTag: endpointTag,
|
||||
ChallengeID: challengeID,
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return E.Cause(err, "cancel openvpn challenge")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *CommandClient) SetTailscaleExitNode(endpointTag string, stableID string) error {
|
||||
_, err := callWithResult(c, func(ctx context.Context, client daemon.StartedServiceClient) (*emptypb.Empty, error) {
|
||||
return client.SetTailscaleExitNode(ctx, &daemon.SetTailscaleExitNodeRequest{
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
package libbox
|
||||
|
||||
import (
|
||||
"github.com/sagernet/sing-box/daemon"
|
||||
"github.com/sagernet/sing/common"
|
||||
)
|
||||
|
||||
type OpenConnectStatusUpdate struct {
|
||||
endpoints []*OpenConnectEndpointStatus
|
||||
}
|
||||
|
||||
func (u *OpenConnectStatusUpdate) Endpoints() OpenConnectEndpointStatusIterator {
|
||||
return newIterator(u.endpoints)
|
||||
}
|
||||
|
||||
type OpenConnectEndpointStatusIterator interface {
|
||||
Next() *OpenConnectEndpointStatus
|
||||
HasNext() bool
|
||||
}
|
||||
|
||||
type OpenConnectEndpointStatus struct {
|
||||
EndpointTag string
|
||||
State string
|
||||
AuthForm *OpenConnectAuthForm
|
||||
Error string
|
||||
TunnelInfo *OpenConnectTunnelInfo
|
||||
}
|
||||
|
||||
type OpenConnectTunnelInfo struct {
|
||||
Server string
|
||||
Flavor string
|
||||
Transport string
|
||||
ipv4 []string
|
||||
ipv6 []string
|
||||
dns []string
|
||||
MTU int32
|
||||
ConnectedSince int64
|
||||
}
|
||||
|
||||
func (i *OpenConnectTunnelInfo) IPv4() StringIterator {
|
||||
return newIterator(i.ipv4)
|
||||
}
|
||||
|
||||
func (i *OpenConnectTunnelInfo) IPv6() StringIterator {
|
||||
return newIterator(i.ipv6)
|
||||
}
|
||||
|
||||
func (i *OpenConnectTunnelInfo) DNS() StringIterator {
|
||||
return newIterator(i.dns)
|
||||
}
|
||||
|
||||
type OpenConnectAuthForm struct {
|
||||
ID string
|
||||
Banner string
|
||||
Message string
|
||||
Error string
|
||||
URL string
|
||||
fields []*OpenConnectAuthFormField
|
||||
}
|
||||
|
||||
func (f *OpenConnectAuthForm) Fields() OpenConnectAuthFormFieldIterator {
|
||||
return newIterator(f.fields)
|
||||
}
|
||||
|
||||
type OpenConnectAuthFormFieldIterator interface {
|
||||
Next() *OpenConnectAuthFormField
|
||||
HasNext() bool
|
||||
}
|
||||
|
||||
type OpenConnectAuthFormField struct {
|
||||
SubmissionKey string
|
||||
Name string
|
||||
Label string
|
||||
Kind string
|
||||
Value string
|
||||
options []*OpenConnectAuthFormChoice
|
||||
}
|
||||
|
||||
func (f *OpenConnectAuthFormField) Options() OpenConnectAuthFormChoiceIterator {
|
||||
return newIterator(f.options)
|
||||
}
|
||||
|
||||
type OpenConnectAuthFormChoiceIterator interface {
|
||||
Next() *OpenConnectAuthFormChoice
|
||||
HasNext() bool
|
||||
}
|
||||
|
||||
type OpenConnectAuthFormChoice struct {
|
||||
Value string
|
||||
Label string
|
||||
}
|
||||
|
||||
type OpenConnectFormValues struct {
|
||||
values map[string]string
|
||||
}
|
||||
|
||||
func NewOpenConnectFormValues() *OpenConnectFormValues {
|
||||
return &OpenConnectFormValues{values: make(map[string]string)}
|
||||
}
|
||||
|
||||
func (v *OpenConnectFormValues) Add(key string, value string) {
|
||||
v.values[key] = value
|
||||
}
|
||||
|
||||
type OpenConnectStatusHandler interface {
|
||||
OnStatusUpdate(status *OpenConnectStatusUpdate)
|
||||
OnError(message string)
|
||||
}
|
||||
|
||||
type OpenConnectStatusSubscription struct {
|
||||
streamSession
|
||||
}
|
||||
|
||||
func openConnectStatusUpdateFromGRPC(update *daemon.OpenConnectStatusUpdate) *OpenConnectStatusUpdate {
|
||||
return &OpenConnectStatusUpdate{
|
||||
endpoints: common.Map(update.Endpoints, openConnectEndpointStatusFromGRPC),
|
||||
}
|
||||
}
|
||||
|
||||
func openConnectEndpointStatusFromGRPC(status *daemon.OpenConnectEndpointStatus) *OpenConnectEndpointStatus {
|
||||
result := &OpenConnectEndpointStatus{
|
||||
EndpointTag: status.EndpointTag,
|
||||
State: status.State,
|
||||
Error: status.Error,
|
||||
}
|
||||
if status.AuthForm != nil {
|
||||
fields := common.Map(status.AuthForm.Fields, func(field *daemon.OpenConnectAuthFormField) *OpenConnectAuthFormField {
|
||||
return &OpenConnectAuthFormField{
|
||||
SubmissionKey: field.SubmissionKey,
|
||||
Name: field.Name,
|
||||
Label: field.Label,
|
||||
Kind: field.Kind,
|
||||
Value: field.Value,
|
||||
options: common.Map(field.Options, func(option *daemon.OpenConnectAuthFormChoice) *OpenConnectAuthFormChoice {
|
||||
return &OpenConnectAuthFormChoice{
|
||||
Value: option.Value,
|
||||
Label: option.Label,
|
||||
}
|
||||
}),
|
||||
}
|
||||
})
|
||||
result.AuthForm = &OpenConnectAuthForm{
|
||||
ID: status.AuthForm.Id,
|
||||
Banner: status.AuthForm.Banner,
|
||||
Message: status.AuthForm.Message,
|
||||
Error: status.AuthForm.Error,
|
||||
URL: status.AuthForm.Url,
|
||||
fields: fields,
|
||||
}
|
||||
}
|
||||
if status.TunnelInfo != nil {
|
||||
result.TunnelInfo = &OpenConnectTunnelInfo{
|
||||
Server: status.TunnelInfo.Server,
|
||||
Flavor: status.TunnelInfo.Flavor,
|
||||
Transport: status.TunnelInfo.Transport,
|
||||
ipv4: status.TunnelInfo.Ipv4,
|
||||
ipv6: status.TunnelInfo.Ipv6,
|
||||
dns: status.TunnelInfo.Dns,
|
||||
MTU: int32(status.TunnelInfo.Mtu),
|
||||
ConnectedSince: status.TunnelInfo.ConnectedSince,
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package libbox
|
||||
|
||||
import (
|
||||
"github.com/sagernet/sing-box/daemon"
|
||||
"github.com/sagernet/sing/common"
|
||||
)
|
||||
|
||||
type OpenVPNStatusUpdate struct {
|
||||
endpoints []*OpenVPNEndpointStatus
|
||||
}
|
||||
|
||||
func (u *OpenVPNStatusUpdate) Endpoints() OpenVPNEndpointStatusIterator {
|
||||
return newIterator(u.endpoints)
|
||||
}
|
||||
|
||||
type OpenVPNEndpointStatusIterator interface {
|
||||
Next() *OpenVPNEndpointStatus
|
||||
HasNext() bool
|
||||
}
|
||||
|
||||
type OpenVPNEndpointStatus struct {
|
||||
EndpointTag string
|
||||
State string
|
||||
Challenge *OpenVPNChallenge
|
||||
Error string
|
||||
TunnelInfo *OpenVPNTunnelInfo
|
||||
}
|
||||
|
||||
type OpenVPNTunnelInfo struct {
|
||||
Server string
|
||||
Network string
|
||||
Cipher string
|
||||
ipv4 []string
|
||||
ipv6 []string
|
||||
dns []string
|
||||
MTU int32
|
||||
ConnectedSince int64
|
||||
}
|
||||
|
||||
func (i *OpenVPNTunnelInfo) IPv4() StringIterator {
|
||||
return newIterator(i.ipv4)
|
||||
}
|
||||
|
||||
func (i *OpenVPNTunnelInfo) IPv6() StringIterator {
|
||||
return newIterator(i.ipv6)
|
||||
}
|
||||
|
||||
func (i *OpenVPNTunnelInfo) DNS() StringIterator {
|
||||
return newIterator(i.dns)
|
||||
}
|
||||
|
||||
type OpenVPNChallenge struct {
|
||||
ID string
|
||||
Kind string
|
||||
Username string
|
||||
Message string
|
||||
URL string
|
||||
SecretMessage string
|
||||
Echo bool
|
||||
PreviousError string
|
||||
Deadline int64
|
||||
}
|
||||
|
||||
type OpenVPNChallengeResponse struct {
|
||||
Username string
|
||||
Password string
|
||||
Secret string
|
||||
}
|
||||
|
||||
type OpenVPNStatusHandler interface {
|
||||
OnStatusUpdate(status *OpenVPNStatusUpdate)
|
||||
OnError(message string)
|
||||
}
|
||||
|
||||
type OpenVPNStatusSubscription struct {
|
||||
streamSession
|
||||
}
|
||||
|
||||
func openVPNStatusUpdateFromGRPC(update *daemon.OpenVPNStatusUpdate) *OpenVPNStatusUpdate {
|
||||
return &OpenVPNStatusUpdate{
|
||||
endpoints: common.Map(update.Endpoints, openVPNEndpointStatusFromGRPC),
|
||||
}
|
||||
}
|
||||
|
||||
func openVPNEndpointStatusFromGRPC(status *daemon.OpenVPNEndpointStatus) *OpenVPNEndpointStatus {
|
||||
result := &OpenVPNEndpointStatus{
|
||||
EndpointTag: status.EndpointTag,
|
||||
State: status.State,
|
||||
Error: status.Error,
|
||||
}
|
||||
if status.Challenge != nil {
|
||||
result.Challenge = &OpenVPNChallenge{
|
||||
ID: status.Challenge.Id,
|
||||
Kind: status.Challenge.Kind,
|
||||
Username: status.Challenge.Username,
|
||||
Message: status.Challenge.Message,
|
||||
URL: status.Challenge.Url,
|
||||
SecretMessage: status.Challenge.SecretMessage,
|
||||
Echo: status.Challenge.Echo,
|
||||
PreviousError: status.Challenge.PreviousError,
|
||||
Deadline: status.Challenge.Deadline,
|
||||
}
|
||||
}
|
||||
if status.TunnelInfo != nil {
|
||||
result.TunnelInfo = &OpenVPNTunnelInfo{
|
||||
Server: status.TunnelInfo.Server,
|
||||
Network: status.TunnelInfo.Network,
|
||||
Cipher: status.TunnelInfo.Cipher,
|
||||
ipv4: status.TunnelInfo.Ipv4,
|
||||
ipv6: status.TunnelInfo.Ipv6,
|
||||
dns: status.TunnelInfo.Dns,
|
||||
MTU: int32(status.TunnelInfo.Mtu),
|
||||
ConnectedSince: status.TunnelInfo.ConnectedSince,
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user