Skip to content

Commit

Permalink
增加http_proxy重连逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Mob2003 committed Apr 8, 2023
1 parent b48e9e7 commit bd49fdc
Show file tree
Hide file tree
Showing 26 changed files with 356 additions and 289 deletions.
Binary file modified bin/rakshasa_fullnode_amd64_darwin
Binary file not shown.
Binary file modified bin/rakshasa_fullnode_amd64_linux
Binary file not shown.
Binary file modified bin/rakshasa_fullnode_amd64_win.exe
Binary file not shown.
Binary file modified bin/rakshasa_fullnode_lite_amd64_darwin
Binary file not shown.
Binary file modified bin/rakshasa_fullnode_lite_amd64_linux
Binary file not shown.
Binary file modified bin/rakshasa_fullnode_lite_amd64_win.exe
Binary file not shown.
Binary file modified bin/rakshasa_node_amd64_darwin
Binary file not shown.
Binary file modified bin/rakshasa_node_amd64_linux
Binary file not shown.
Binary file modified bin/rakshasa_node_amd64_win.exe
Binary file not shown.
Binary file modified bin/rakshasa_node_lite_amd64_darwin
Binary file not shown.
Binary file modified bin/rakshasa_node_lite_amd64_linux
Binary file not shown.
Binary file modified bin/rakshasa_node_lite_amd64_win.exe
Binary file not shown.
4 changes: 3 additions & 1 deletion build.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func main() {
if *genfullNodePrivate {
b, _ := os.ReadFile("./cert/private.pem")
data := fmt.Sprintf("package cert\r\n func init(){\r\nprivateKey=%#v\r\n}\r\n", b)
os.WriteFile("./cert/private.go", []byte(data), 0655)
if err := os.WriteFile("./cert/private.go", []byte(data), 0655); err != nil {
fmt.Printf("写入./cert/private.go失败,错误 %v", err)
}
return
}
if *gencert {
Expand Down
41 changes: 30 additions & 11 deletions common/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package common

import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math/rand"
"net"
"rakshasa/aes"
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -195,17 +197,6 @@ func init() {

}

type RegMsg struct {
UUID string //当前机器uuid
RegAddr string //远程连接的addr
Hostname string //当前机器名称
Goos string
ViaUUID string
Err string
MainIp string
Port string
}

var msgId uint32

func (m *Msg) Marshal() []byte {
Expand Down Expand Up @@ -329,3 +320,31 @@ func ResolveTCPAddr(str string) ([]string, error) {

return dst, nil
}
func GetUUIDFromInterfaceMac() string {
ifts, _ := net.Interfaces()
for _, ift := range ifts {
if addr := ift.HardwareAddr.String(); len(addr) > 0 {
var randSeed = make([]byte, 8)
for k, s := range strings.Split(addr, ":") {
if k < 8 {
n, _ := strconv.ParseUint(s, 16, 8)
randSeed[k] = byte(n)
}

}
source := rand.NewSource(int64(binary.LittleEndian.Uint64(randSeed)))
buf := bytes.NewBuffer(nil)
for i := 0; i < 2; i++ {
var b = make([]byte, 8)
binary.LittleEndian.PutUint64(b, uint64(source.Int63()))
buf.Write(b)
}
id, err := uuid.NewRandomFromReader(buf)
if err == nil {
return id.String()
}
}

}
return uuid.New().String()
}
3 changes: 2 additions & 1 deletion common/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package common

type Config struct {
UUID string //以指定uuid启动
DstNode []string //-d 上级节点
Password string //通讯密码,可为空
Port int //默认8883
ListenIp string //指定公网ip,其他节点进行额外节点连接时候,尝试连接的ip
ListenIp string //指定公网ip,其他节点进行额外节点连接时候,尝试连接的ip
Limit bool //禁止额外连接,只连接-d节点,不会尝试连接其他节点
FileName string
FileSave bool `yaml:"-"`
Expand Down
15 changes: 11 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
_ "net/http/pprof"
"rakshasa/aes"
"rakshasa/common"
"rakshasa/httppool"
"rakshasa/server"
"strconv"
"sync"

"github.com/google/uuid"
)

func main() {
Expand Down Expand Up @@ -43,6 +44,8 @@ func main() {
shellCodeTimeout = flag.Int("sTimeout", 3, "shellcode的超时等待时间,默认3秒")
http_proxy = flag.String("http_proxy", "", "以本地http代理服务端模式运行,通过-d的服务器多级代理转出数据,如果没有-d参数,则使用本机进行下一步连接, 用户名:密码@ip:端口 可以省略为端口,如: \r\n -http_proxy admin:[email protected]:8080\r\n -http_proxy admin:12345@8080\r\n -http_proxy 8080")
http_proxy_pool = flag.String("http_proxy_pool", "", "从指定文件读取http代理服务器池,通过最后节点后(不使用-d则为本机),再从该池里读取一个代理进行请求")
withUUID = flag.String("uuid", "", "以指定uuid启动,如果uuid非法或者为空,则以网卡mac方式生成uuid")
randomUUID = flag.Bool("randomUUID", false, "每次启动,都使用随机的uuid")
)

flag.Parse()
Expand All @@ -51,7 +54,7 @@ func main() {
log.Println("检测url不是默认url,将取消匿名代理检测")
*check_proxy_anonymous = false
}
httppool.CheckProxy(*check_proxy, *check_proxy_out, *check_proxy_timeout, *check_proxy_url, *check_proxy_anonymous)
server.CheckProxy(*check_proxy, *check_proxy_out, *check_proxy_timeout, *check_proxy_url, *check_proxy_anonymous)
return
}

Expand All @@ -69,7 +72,11 @@ func main() {
}

}

if *randomUUID {
config.UUID = uuid.New().String()
} else if *withUUID != "" {
config.UUID = *withUUID
}
if *dstNode != "" {
serverlist, err := common.ResolveTCPAddr(*dstNode)
if err != nil {
Expand Down Expand Up @@ -133,7 +140,7 @@ func main() {
if *shellCode != "" {
server.RunShellcodeWithDst(*dstNode, *shellCode, *shellCodeXorKey, *shellCodeParam, *shellCodeTimeout)
}
if err := server.StartServer(fmt.Sprintf(":%d", config.Port)); err != nil {
if err := server.StartServer(fmt.Sprintf(":%d", config.Port)); err != nil {
log.Fatalln(err)
}

Expand Down
53 changes: 1 addition & 52 deletions httppool/check_proxy.go → server/check_proxy.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package httppool
package server

import (
"bufio"
"errors"
"fmt"
"io"
"log"
"net"
Expand Down Expand Up @@ -160,53 +158,4 @@ func check(cfg *common.Addr, timeout uint, checkurl string, outFile *os.File, an
return false
}

type HttpPool struct {
r *bufio.Reader
f *os.File
sync.Mutex
}

func HttpPoolInit(file string) (*HttpPool, error) {
f, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("打开http代理池文件 %s 失败", file)
}
p := &HttpPool{
r: bufio.NewReader(f),
f: f,
Mutex: sync.Mutex{},
}
if _, err = p.do_next(0); err != nil {
return nil, fmt.Errorf("无法从%s文件获取代理,错误%v", file, err)
}
return p, nil
}
func (p *HttpPool) Next() *common.Addr {
addr, _ := p.do_next(0)
return addr
}
func (p *HttpPool) do_next(n int) (*common.Addr, error) {
if n > 100 {
return nil, errors.New("重试错误次数过多")
}
p.Lock()
line, err := p.r.ReadString(10)
if err == io.EOF {
p.f.Seek(0, 0)
p.r.Reset(p.f)
p.Unlock()
return p.do_next(n + 1)
}
p.Unlock()
line = strings.TrimRight(line, "\n")
line = strings.TrimRight(line, "\r")

if len(line) == 0 {
return p.do_next(n + 1)
}
addr, err := common.ParseAddr(line)
if err != nil {
return p.do_next(n + 1)
}
return addr, nil
}
19 changes: 18 additions & 1 deletion server/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func cliInit() *ishell.Shell {
return
}
for _, addr := range strings.Split(c.Args[0], ",") {
_, err := connectNew(addr)
_, err := getNode(addr)
if err != nil {
c.Println("连接", addr, "失败", err)
return
Expand Down Expand Up @@ -106,6 +106,23 @@ func cliInit() *ishell.Shell {

},
})
shell.AddCmd(&ishell.Cmd{
Name: "closenode",
Help: "关闭一个node ID",
Func: func(c *ishell.Context) {
l := clientLock.Lock()
defer l.Unlock()
if len(c.Args) != 1 {
c.Println("参数不对")
return
}
n, ok := nodeMap[c.Args[0]]
if ok {
n.Close("debug关闭")
}

},
})
}
return shell
}
10 changes: 9 additions & 1 deletion server/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package server

import (
"gopkg.in/yaml.v3"
"io/ioutil"
"rakshasa/common"

"github.com/google/uuid"
"gopkg.in/yaml.v3"
)

var currentConfig common.Config
Expand All @@ -13,6 +15,12 @@ func SetConfig(config common.Config) {
currentConfig.FileSave = false
currentNode.mainIp = currentConfig.ListenIp
currentNode.port = currentConfig.Port
if id, err := uuid.Parse(currentConfig.UUID); err != nil {
currentConfig.UUID = common.GetUUIDFromInterfaceMac()
}else{
currentConfig.UUID=id.String()
}
currentNode.uuid = currentConfig.UUID
}
func ConfigSave() error {
b, _ := yaml.Marshal(currentConfig)
Expand Down
12 changes: 8 additions & 4 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ type Conn struct {
close chan string
isClient bool
nodeConn *tls.Conn
regResult chan error
regResultNode chan *node
regResult chan RegMsg
}

type serverListen struct {
Expand Down Expand Up @@ -544,7 +543,7 @@ func (c *Conn) handlerNodeRead() {
}
}
case <-time.After(common.CMD_TIMEOUT):
newNode.Delete("超时")
newNode.Close("超时")
}
}()

Expand Down Expand Up @@ -660,6 +659,7 @@ func (c *Conn) handle() {
if common.Debug {
fmt.Println(c.nodeConn.RemoteAddr().String(), "关闭原因", reason)
}

if c.nodeConn != nil {
if common.Debug {
fmt.Println("執行close1")
Expand All @@ -668,13 +668,17 @@ func (c *Conn) handle() {
}

if c.node != nil {
c.node.Close(reason)
//移除上游连接
for i := len(upLevelNode) - 1; i >= 0; i-- {
n := upLevelNode[i]
if n.uuid == c.node.uuid {
upLevelNode = append(upLevelNode[:i], upLevelNode[i+1:]...)
}
}
if common.Debug {
fmt.Println("upLevelNode",len(upLevelNode))
}
}

}
Expand All @@ -690,7 +694,7 @@ func (c *Conn) handle() {
func (c *Conn) reg() error {

var err error
reg := &common.RegMsg{
reg := &RegMsg{
UUID: currentNode.uuid,
MainIp: cert.RSAEncrypterStr(currentNode.mainIp),
Port: cert.RSAEncrypterStr(strconv.Itoa(currentNode.port)),
Expand Down
Loading

0 comments on commit bd49fdc

Please sign in to comment.