github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/endpoints/nat.go (about)

     1  /*
     2   * Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package endpoints
    19  
    20  import (
    21  	"context"
    22  
    23  	"github.com/mysteriumnetwork/node/core/monitoring"
    24  
    25  	"github.com/gin-gonic/gin"
    26  	"github.com/mysteriumnetwork/go-rest/apierror"
    27  
    28  	"github.com/mysteriumnetwork/node/nat"
    29  	"github.com/mysteriumnetwork/node/tequilapi/contract"
    30  	"github.com/mysteriumnetwork/node/tequilapi/utils"
    31  )
    32  
    33  // NATEndpoint struct represents endpoints about NAT traversal
    34  type NATEndpoint struct {
    35  	stateProvider stateProvider
    36  	natProber     natProber
    37  }
    38  
    39  type natProber interface {
    40  	Probe(context.Context) (nat.NATType, error)
    41  }
    42  
    43  type nodeStatusProvider interface {
    44  	Status() monitoring.Status
    45  }
    46  
    47  // NewNATEndpoint creates and returns nat endpoint
    48  func NewNATEndpoint(stateProvider stateProvider, natProber natProber) *NATEndpoint {
    49  	return &NATEndpoint{
    50  		stateProvider: stateProvider,
    51  		natProber:     natProber,
    52  	}
    53  }
    54  
    55  // NATType provides NAT type in terms of traversal capabilities
    56  // swagger:operation GET /nat/type NAT NATTypeDTO
    57  //
    58  //	---
    59  //	summary: Shows NAT type in terms of traversal capabilities.
    60  //	description: Returns NAT type. May produce invalid result while VPN connection is established
    61  //	responses:
    62  //	  200:
    63  //	    description: NAT type
    64  //	    schema:
    65  //	      "$ref": "#/definitions/NATTypeDTO"
    66  //	  500:
    67  //	    description: Internal server error
    68  //	    schema:
    69  //	      "$ref": "#/definitions/APIError"
    70  func (ne *NATEndpoint) NATType(c *gin.Context) {
    71  	res, err := ne.natProber.Probe(c.Request.Context())
    72  	if err != nil {
    73  		c.Error(apierror.Internal("NAT probe failed", contract.ErrCodeNATProbe))
    74  		return
    75  	}
    76  	utils.WriteAsJSON(contract.NATTypeDTO{
    77  		Type:  res,
    78  		Error: "",
    79  	}, c.Writer)
    80  }
    81  
    82  // AddRoutesForNAT adds nat routes to given router
    83  func AddRoutesForNAT(stateProvider stateProvider, natProber natProber) func(*gin.Engine) error {
    84  	natEndpoint := NewNATEndpoint(stateProvider, natProber)
    85  
    86  	return func(e *gin.Engine) error {
    87  		v1Group := e.Group("/nat")
    88  		{
    89  			v1Group.GET("/type", natEndpoint.NATType)
    90  		}
    91  		return nil
    92  	}
    93  }