11package control
22
33import (
4- "context"
54 "github.com/Driver-C/tryssh/pkg/config"
65 "github.com/Driver-C/tryssh/pkg/launcher"
76 "github.com/Driver-C/tryssh/pkg/utils"
87 "strings"
9- "sync"
108 "time"
119)
1210
@@ -79,12 +77,18 @@ func (cc *ScpController) tryCopyWithCache(user string, targetServer *config.Serv
7977
8078func (cc * ScpController ) tryCopyWithoutCache (user string ) {
8179 combinations := config .GenerateCombination (cc .destIp , user , cc .configuration )
82- launchers := launcher .NewScpLaunchersByCombinations (combinations , cc .source , cc .destination , cc .recursive , cc .sshTimeout )
83- hitLaunchers := concurrencyScpTryToConnect (cc .concurrency , launchers )
80+ launchers := launcher .NewScpLaunchersByCombinations (combinations , cc .source , cc .destination ,
81+ cc .recursive , cc .sshTimeout )
82+ connectors := make ([]launcher.Connector , len (launchers ))
83+ for i , l := range launchers {
84+ connectors [i ] = l
85+ }
86+ hitLaunchers := ConcurrencyTryToConnect (cc .concurrency , connectors )
8487 if hitLaunchers != nil {
8588 utils .Logger .Infoln ("Login succeeded. The cache will be added.\n " )
89+ hitLauncher := hitLaunchers [0 ].(* launcher.ScpLauncher )
8690 // The new server cache information
87- newServerCache := launcher .GetConfigFromSshConnector (& hitLaunchers [ 0 ] .SshConnector )
91+ newServerCache := launcher .GetConfigFromSshConnector (& hitLauncher .SshConnector )
8892 // Determine if the login attempt was successful after the old cache login failed.
8993 // If so, delete the old cache information that cannot be logged in after the login attempt is successful
9094 if cc .cacheIsFound {
@@ -100,10 +104,10 @@ func (cc *ScpController) tryCopyWithoutCache(user string) {
100104 utils .Logger .Infoln ("Cache added.\n \n " )
101105 // If the timeout time is less than sshClientTimeoutWhenLogin during login,
102106 // change to sshClientTimeoutWhenLogin
103- if hitLaunchers [ 0 ] .SshTimeout < sshClientTimeoutWhenLogin {
104- hitLaunchers [ 0 ] .SshTimeout = sshClientTimeoutWhenLogin
107+ if hitLauncher .SshTimeout < sshClientTimeoutWhenLogin {
108+ hitLauncher .SshTimeout = sshClientTimeoutWhenLogin
105109 }
106- if ! hitLaunchers [ 0 ] .Launch () {
110+ if ! hitLauncher .Launch () {
107111 utils .Logger .Errorf ("Login failed.\n " )
108112 }
109113 } else {
@@ -122,59 +126,6 @@ func (cc *ScpController) searchAliasExistsOrNot() {
122126 }
123127}
124128
125- func concurrencyScpTryToConnect (concurrency int , launchers []* launcher.ScpLauncher ) []* launcher.ScpLauncher {
126- var hitLaunchers []* launcher.ScpLauncher
127- var mutex sync.Mutex
128- var hostKeyMutex sync.Mutex
129- // If the number of launchers is less than the set concurrency, change the concurrency to the number of launchers
130- if concurrency > len (launchers ) {
131- concurrency = len (launchers )
132- }
133- launchersChan := make (chan * launcher.ScpLauncher )
134- ctx , cancelFunc := context .WithCancel (context .Background ())
135- // Producer
136- go func (ctx context.Context , launchersChan chan <- * launcher.ScpLauncher , launchers []* launcher.ScpLauncher ) {
137- for _ , launcherP := range launchers {
138- select {
139- case <- ctx .Done ():
140- break
141- default :
142- launchersChan <- launcherP
143- }
144- }
145- close (launchersChan )
146- }(ctx , launchersChan , launchers )
147- // Consumer
148- var wg sync.WaitGroup
149- for i := 0 ; i < concurrency ; i ++ {
150- wg .Add (1 )
151- go func (ctx context.Context , cancelFunc context.CancelFunc ,
152- launchersChan <- chan * launcher.ScpLauncher , cwg * sync.WaitGroup , mutex * sync.Mutex ) {
153- defer cwg .Done ()
154- for {
155- select {
156- case <- ctx .Done ():
157- return
158- case launcherP , ok := <- launchersChan :
159- if ! ok {
160- return
161- }
162- launcherP .HostKeyMutex = & hostKeyMutex
163- if err := launcherP .TryToConnect (); err == nil {
164- mutex .Lock ()
165- hitLaunchers = append (hitLaunchers , launcherP )
166- mutex .Unlock ()
167- cancelFunc ()
168- }
169- }
170- }
171- }(ctx , cancelFunc , launchersChan , & wg , & mutex )
172- }
173- wg .Wait ()
174- cancelFunc ()
175- return hitLaunchers
176- }
177-
178129func NewScpController (source string , destination string , configuration * config.MainConfig ) * ScpController {
179130 return & ScpController {
180131 source : source ,
0 commit comments