mirror of
				https://github.com/nadoo/glider.git
				synced 2025-11-04 07:42:38 +08:00 
			
		
		
		
	fix(ssh): optimized the connection initialization
This commit is contained in:
		
							parent
							
								
									1e01d8692d
								
							
						
					
					
						commit
						1c0106ce6b
					
				
							
								
								
									
										12
									
								
								.github/workflows/build.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										12
									
								
								.github/workflows/build.yml
									
									
									
									
										vendored
									
									
								
							@ -35,7 +35,7 @@ jobs:
 | 
				
			|||||||
          go-version: ${{ env.GO_MOD_VERSION}}
 | 
					          go-version: ${{ env.GO_MOD_VERSION}}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: Set up Cache
 | 
					      - name: Set up Cache
 | 
				
			||||||
        uses: actions/cache@v2
 | 
					        uses: actions/cache@v3
 | 
				
			||||||
        with:
 | 
					        with:
 | 
				
			||||||
          path: |
 | 
					          path: |
 | 
				
			||||||
            ~/go/pkg/mod
 | 
					            ~/go/pkg/mod
 | 
				
			||||||
@ -99,16 +99,16 @@ jobs:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      - name: Docker - Set up Buildx
 | 
					      - name: Docker - Set up Buildx
 | 
				
			||||||
        id: buildx
 | 
					        id: buildx
 | 
				
			||||||
        uses: docker/setup-buildx-action@v1
 | 
					        uses: docker/setup-buildx-action@v2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: Docker - Login to DockerHub
 | 
					      - name: Docker - Login to DockerHub
 | 
				
			||||||
        uses: docker/login-action@v1
 | 
					        uses: docker/login-action@v2
 | 
				
			||||||
        with:
 | 
					        with:
 | 
				
			||||||
          username: ${{ secrets.DOCKERHUB_USERNAME }}
 | 
					          username: ${{ secrets.DOCKERHUB_USERNAME }}
 | 
				
			||||||
          password: ${{ secrets.DOCKERHUB_TOKEN }}
 | 
					          password: ${{ secrets.DOCKERHUB_TOKEN }}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: Docker - Login to GHCR
 | 
					      - name: Docker - Login to GHCR
 | 
				
			||||||
        uses: docker/login-action@v1
 | 
					        uses: docker/login-action@v2
 | 
				
			||||||
        with:
 | 
					        with:
 | 
				
			||||||
          registry: ghcr.io
 | 
					          registry: ghcr.io
 | 
				
			||||||
          username: ${{ github.repository_owner }}
 | 
					          username: ${{ github.repository_owner }}
 | 
				
			||||||
@ -116,7 +116,7 @@ jobs:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      - name: Docker - Docker meta
 | 
					      - name: Docker - Docker meta
 | 
				
			||||||
        id: meta
 | 
					        id: meta
 | 
				
			||||||
        uses: docker/metadata-action@v3
 | 
					        uses: docker/metadata-action@v4
 | 
				
			||||||
        with:
 | 
					        with:
 | 
				
			||||||
          images: |
 | 
					          images: |
 | 
				
			||||||
            ${{ env.DOCKERHUB_REPO }}
 | 
					            ${{ env.DOCKERHUB_REPO }}
 | 
				
			||||||
@ -127,7 +127,7 @@ jobs:
 | 
				
			|||||||
            type=semver,pattern={{major}}.{{minor}}
 | 
					            type=semver,pattern={{major}}.{{minor}}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: Docker - Build and push
 | 
					      - name: Docker - Build and push
 | 
				
			||||||
        uses: docker/build-push-action@v2
 | 
					        uses: docker/build-push-action@v3
 | 
				
			||||||
        with:
 | 
					        with:
 | 
				
			||||||
          context: .
 | 
					          context: .
 | 
				
			||||||
          file: .Dockerfile
 | 
					          file: .Dockerfile
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							@ -11,7 +11,7 @@ require (
 | 
				
			|||||||
	github.com/nadoo/conflag v0.3.1
 | 
						github.com/nadoo/conflag v0.3.1
 | 
				
			||||||
	github.com/nadoo/ipset v0.5.0
 | 
						github.com/nadoo/ipset v0.5.0
 | 
				
			||||||
	github.com/xtaci/kcp-go/v5 v5.6.1
 | 
						github.com/xtaci/kcp-go/v5 v5.6.1
 | 
				
			||||||
	golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f
 | 
						golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122
 | 
				
			||||||
	golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6
 | 
						golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.sum
									
									
									
									
									
								
							@ -111,8 +111,8 @@ golang.org/x/crypto v0.0.0-20191219195013-becbf705a915/go.mod h1:LzIPMQfyMNhhGPh
 | 
				
			|||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
 | 
					golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
 | 
				
			||||||
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
 | 
					golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
 | 
				
			||||||
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
 | 
					golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
 | 
				
			||||||
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f h1:OeJjE6G4dgCY4PIXvIRQbE8+RX+uXZyGhUy/ksMGJoc=
 | 
					golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122 h1:NvGWuYG8dkDHFSKksI1P9faiVJ9rayE6l0+ouWVIDs8=
 | 
				
			||||||
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
 | 
					golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
 | 
				
			||||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
 | 
					golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
 | 
				
			||||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
 | 
					golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
 | 
				
			||||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
 | 
					golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
 | 
				
			||||||
 | 
				
			|||||||
@ -21,11 +21,12 @@ type SSH struct {
 | 
				
			|||||||
	proxy  proxy.Proxy
 | 
						proxy  proxy.Proxy
 | 
				
			||||||
	addr   string
 | 
						addr   string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mu     sync.RWMutex
 | 
					 | 
				
			||||||
	once   sync.Once
 | 
					 | 
				
			||||||
	conn   net.Conn
 | 
						conn   net.Conn
 | 
				
			||||||
	client *ssh.Client
 | 
						client *ssh.Client
 | 
				
			||||||
	config *ssh.ClientConfig
 | 
						config *ssh.ClientConfig
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						once  sync.Once
 | 
				
			||||||
 | 
						mutex sync.RWMutex
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
@ -105,10 +106,10 @@ func (s *SSH) Addr() string {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Dial connects to the address addr on the network net via the proxy.
 | 
					// Dial connects to the address addr on the network net via the proxy.
 | 
				
			||||||
func (s *SSH) Dial(network, addr string) (net.Conn, error) {
 | 
					func (s *SSH) Dial(network, addr string) (net.Conn, error) {
 | 
				
			||||||
	s.once.Do(func() { go s.keepConn() })
 | 
						s.once.Do(func() { go s.keepConn(s.initConn() == nil) })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	s.mu.RLock()
 | 
						s.mutex.RLock()
 | 
				
			||||||
	defer s.mu.RUnlock()
 | 
						defer s.mutex.RUnlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if s.client == nil {
 | 
						if s.client == nil {
 | 
				
			||||||
		return nil, errors.New("ssh client is nil")
 | 
							return nil, errors.New("ssh client is nil")
 | 
				
			||||||
@ -123,11 +124,11 @@ func (s *SSH) dial(network, addr string) (net.Conn, error) {
 | 
				
			|||||||
	return c, err
 | 
						return c, err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (s *SSH) connect() error {
 | 
					func (s *SSH) initConn() error {
 | 
				
			||||||
	s.mu.Lock()
 | 
						s.mutex.Lock()
 | 
				
			||||||
	defer s.mu.Unlock()
 | 
						defer s.mutex.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	log.F("[ssh] trying connecting to %s", s.addr)
 | 
						log.F("[ssh] connecting to %s", s.addr)
 | 
				
			||||||
	c, err := s.dialer.Dial("tcp", s.addr)
 | 
						c, err := s.dialer.Dial("tcp", s.addr)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.F("[ssh] dial connection to %s error: %s", s.addr, err)
 | 
							log.F("[ssh] dial connection to %s error: %s", s.addr, err)
 | 
				
			||||||
@ -148,10 +149,15 @@ func (s *SSH) connect() error {
 | 
				
			|||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (s *SSH) keepConn() {
 | 
					func (s *SSH) keepConn(connected bool) {
 | 
				
			||||||
 | 
						if connected {
 | 
				
			||||||
 | 
							s.client.Conn.Wait()
 | 
				
			||||||
 | 
							s.conn.Close()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sleep := time.Second
 | 
						sleep := time.Second
 | 
				
			||||||
	for {
 | 
						for {
 | 
				
			||||||
		if err := s.connect(); err != nil {
 | 
							if err := s.initConn(); err != nil {
 | 
				
			||||||
			sleep *= 2
 | 
								sleep *= 2
 | 
				
			||||||
			if sleep > time.Second*60 {
 | 
								if sleep > time.Second*60 {
 | 
				
			||||||
				sleep = time.Second * 60
 | 
									sleep = time.Second * 60
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user