|
4 | 4 | "bufio" |
5 | 5 | "fmt" |
6 | 6 | "github.com/labstack/gommon/log" |
| 7 | + "github.com/pkg/errors" |
7 | 8 | "github.com/spf13/viper" |
8 | 9 | "io" |
9 | 10 | "net" |
|
33 | 34 | stopChan chan bool |
34 | 35 | errorChan chan error |
35 | 36 | retries time.Duration |
| 37 | + ID string `json:"id"` |
36 | 38 | Name string `json:"name"` |
| 39 | + Random bool `json:"random"` |
37 | 40 | User string |
38 | 41 | Host string |
39 | 42 | TargetAddress string `json:"target_address"` |
@@ -100,7 +103,7 @@ func (s *Server) newConnection(req *ConnectRequest) (c *Connection, err error) { |
100 | 103 | if c.Configuration.Protocol != ProtocolHTTPS { |
101 | 104 | c.RemotePort = 0 |
102 | 105 | } |
103 | | - if err = c.create(); err != nil { |
| 106 | + if err = c.save(); err != nil { |
104 | 107 | return |
105 | 108 | } |
106 | 109 | c.User += ",name=" + c.Name |
@@ -285,36 +288,49 @@ func (c *Connection) stop() { |
285 | 288 | } |
286 | 289 | } |
287 | 290 |
|
| 291 | +func (c *Connection) save() error { |
| 292 | + if c.ID == "" { |
| 293 | + return c.create() |
| 294 | + } |
| 295 | + return c.update() |
| 296 | +} |
| 297 | + |
288 | 298 | func (c *Connection) create() error { |
289 | 299 | e := new(Error) |
290 | | - _, err := c.server.resty.R(). |
| 300 | + res, err := c.server.resty.R(). |
291 | 301 | SetBody(c). |
292 | 302 | SetResult(c). |
293 | 303 | SetError(e). |
294 | 304 | Post("/connections") |
295 | | - return err |
| 305 | + if err != nil { |
| 306 | + return err |
| 307 | + } else if res.IsError() { |
| 308 | + return errors.Errorf("failed to create a connection: error=%s", e.Message) |
| 309 | + } |
| 310 | + return nil |
296 | 311 | } |
297 | 312 |
|
298 | | -func (c *Connection) update() { |
| 313 | +func (c *Connection) update() error { |
299 | 314 | e := new(Error) |
300 | 315 | res, err := c.server.resty.R(). |
301 | 316 | SetBody(c). |
302 | 317 | SetResult(c). |
303 | 318 | SetError(e). |
304 | | - Put("/connections/" + c.Name) |
| 319 | + Put("/connections/" + c.ID) |
305 | 320 | if err != nil { |
306 | | - log.Error(err) |
| 321 | + return err |
307 | 322 | } else if res.IsError() { |
308 | | - log.Errorf("failed to update the connection: name=%s, error=%s", c.Name, e.Message) |
| 323 | + return errors.Errorf("failed to update the connection: name=%s, error=%s", c.Name, e.Message) |
309 | 324 | } |
| 325 | + return nil |
310 | 326 | } |
311 | 327 |
|
312 | 328 | func (c *Connection) delete() error { |
313 | 329 | log.Warnf("removing connection %s", c.Name) |
314 | 330 | e := new(Error) |
315 | 331 | res, err := c.server.resty.R(). |
316 | 332 | SetError(e). |
317 | | - Delete("/connections/" + c.Name) |
| 333 | + Delete("/connections/" + c.ID) |
318 | 334 | if err != nil { |
319 | 335 | return err |
320 | 336 | } else if res.IsError() { |
|
0 commit comments