github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/components/tools/tools.go (about)

     1  package tools
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"net/http"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/Asutorufa/yuhaiin/pkg/components/config"
    11  	"github.com/Asutorufa/yuhaiin/pkg/net/netapi"
    12  	pc "github.com/Asutorufa/yuhaiin/pkg/protos/config"
    13  	"github.com/Asutorufa/yuhaiin/pkg/protos/tools"
    14  	"github.com/Asutorufa/yuhaiin/pkg/utils/relay"
    15  	"google.golang.org/protobuf/types/known/emptypb"
    16  	"google.golang.org/protobuf/types/known/wrapperspb"
    17  )
    18  
    19  type Tools struct {
    20  	tools.UnimplementedToolsServer
    21  	setting  config.Setting
    22  	dialer   netapi.Proxy
    23  	callback func(*pc.Setting)
    24  }
    25  
    26  func NewTools(dialer netapi.Proxy, setting config.Setting, callback func(st *pc.Setting)) *Tools {
    27  	return &Tools{
    28  		setting:  setting,
    29  		dialer:   dialer,
    30  		callback: callback,
    31  	}
    32  }
    33  
    34  func (t *Tools) SaveRemoteBypassFile(ctx context.Context, url *wrapperspb.StringValue) (*emptypb.Empty, error) {
    35  	http := &http.Client{
    36  		Transport: &http.Transport{
    37  			DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
    38  				add, err := netapi.ParseAddress(netapi.PaseNetwork(network), addr)
    39  				if err != nil {
    40  					return nil, err
    41  				}
    42  				return t.dialer.Conn(ctx, add)
    43  			},
    44  		},
    45  	}
    46  
    47  	st, err := t.setting.Load(context.TODO(), &emptypb.Empty{})
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	if err := os.MkdirAll(filepath.Dir(st.Bypass.BypassFile), os.ModeDir); err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	resp, err := http.Get(url.Value)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	defer resp.Body.Close()
    61  
    62  	f, err := os.OpenFile(st.Bypass.BypassFile, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	_, err = relay.Copy(f, resp.Body)
    68  	_ = f.Close()
    69  
    70  	if err == nil {
    71  		t.callback(st)
    72  	}
    73  
    74  	return &emptypb.Empty{}, err
    75  }
    76  
    77  func (t *Tools) GetInterface(context.Context, *emptypb.Empty) (*tools.Interfaces, error) {
    78  	is := &tools.Interfaces{}
    79  	iis, err := net.Interfaces()
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	for _, i := range iis {
    84  		if i.Flags&net.FlagLoopback != 0 {
    85  			continue
    86  		}
    87  		iif := &tools.Interface{
    88  			Name: i.Name,
    89  		}
    90  
    91  		addresses, err := i.Addrs()
    92  		if err == nil {
    93  			for _, a := range addresses {
    94  				iif.Addresses = append(iif.Addresses, a.String())
    95  			}
    96  		}
    97  		is.Interfaces = append(is.Interfaces, iif)
    98  	}
    99  
   100  	return is, nil
   101  }