github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/endpoints/connection_location.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  	"strconv"
    22  
    23  	"github.com/gin-gonic/gin"
    24  
    25  	"github.com/mysteriumnetwork/go-rest/apierror"
    26  	"github.com/mysteriumnetwork/node/core/ip"
    27  	"github.com/mysteriumnetwork/node/core/location"
    28  	"github.com/mysteriumnetwork/node/core/location/locationstate"
    29  	"github.com/mysteriumnetwork/node/tequilapi/contract"
    30  	"github.com/mysteriumnetwork/node/tequilapi/utils"
    31  )
    32  
    33  func locationToRes(l locationstate.Location) contract.LocationDTO {
    34  	return contract.LocationDTO{
    35  		IP:        l.IP,
    36  		ASN:       l.ASN,
    37  		ISP:       l.ISP,
    38  		Continent: l.Continent,
    39  		Country:   l.Country,
    40  		Region:    l.Region,
    41  		City:      l.City,
    42  		IPType:    l.IPType,
    43  	}
    44  }
    45  
    46  // ConnectionLocationEndpoint struct represents /connection/location resource and it's subresources.
    47  type ConnectionLocationEndpoint struct {
    48  	ipResolver             ip.Resolver
    49  	locationResolver       location.Resolver
    50  	locationOriginResolver location.OriginResolver
    51  }
    52  
    53  // NewConnectionLocationEndpoint creates and returns connection location endpoint.
    54  func NewConnectionLocationEndpoint(
    55  	ipResolver ip.Resolver,
    56  	locationResolver location.Resolver,
    57  	locationOriginResolver location.OriginResolver,
    58  ) *ConnectionLocationEndpoint {
    59  	return &ConnectionLocationEndpoint{
    60  		ipResolver:             ipResolver,
    61  		locationResolver:       locationResolver,
    62  		locationOriginResolver: locationOriginResolver,
    63  	}
    64  }
    65  
    66  // GetConnectionIP responds with current ip, using its ip resolver
    67  // swagger:operation GET /connection/ip Connection getConnectionIP
    68  //
    69  //	---
    70  //	summary: Returns IP address
    71  //	description: Returns current public IP address
    72  //	responses:
    73  //	  200:
    74  //	    description: Public IP address
    75  //	    schema:
    76  //	      "$ref": "#/definitions/IPDTO"
    77  //	  503:
    78  //	    description: Service unavailable
    79  //	    schema:
    80  //	      "$ref": "#/definitions/APIError"
    81  func (le *ConnectionLocationEndpoint) GetConnectionIP(c *gin.Context) {
    82  	ipAddress, err := le.ipResolver.GetPublicIP()
    83  	if err != nil {
    84  		c.Error(apierror.ServiceUnavailable())
    85  		return
    86  	}
    87  
    88  	response := contract.IPDTO{
    89  		IP: ipAddress,
    90  	}
    91  	utils.WriteAsJSON(response, c.Writer)
    92  }
    93  
    94  // GetProxyIP responds with proxy ip, using its ip resolver
    95  // swagger:operation GET /connection/proxy/ip Connection getProxyIP
    96  //
    97  //	---
    98  //	summary: Returns IP address
    99  //	description: Returns proxy public IP address
   100  //	responses:
   101  //	  200:
   102  //	    description: Public IP address
   103  //	    schema:
   104  //	      "$ref": "#/definitions/IPDTO"
   105  //	  503:
   106  //	    description: Service unavailable
   107  //	    schema:
   108  //	      "$ref": "#/definitions/APIError"
   109  func (le *ConnectionLocationEndpoint) GetProxyIP(c *gin.Context) {
   110  	n, _ := strconv.Atoi(c.Query("port"))
   111  	ipAddress, err := le.ipResolver.GetProxyIP(n)
   112  	if err != nil {
   113  		c.Error(apierror.ServiceUnavailable())
   114  		return
   115  	}
   116  
   117  	response := contract.IPDTO{
   118  		IP: ipAddress,
   119  	}
   120  	utils.WriteAsJSON(response, c.Writer)
   121  }
   122  
   123  // GetConnectionLocation responds with current connection location
   124  // swagger:operation GET /connection/location Connection getConnectionLocation
   125  //
   126  //	---
   127  //	summary: Returns connection location
   128  //	description: Returns connection locations
   129  //	responses:
   130  //	  200:
   131  //	    description: Connection locations
   132  //	    schema:
   133  //	      "$ref": "#/definitions/LocationDTO"
   134  //	  503:
   135  //	    description: Service unavailable
   136  //	    schema:
   137  //	      "$ref": "#/definitions/APIError"
   138  func (le *ConnectionLocationEndpoint) GetConnectionLocation(c *gin.Context) {
   139  	currentLocation, err := le.locationResolver.DetectLocation()
   140  	if err != nil {
   141  		c.Error(apierror.ServiceUnavailable())
   142  		return
   143  	}
   144  	utils.WriteAsJSON(locationToRes(currentLocation), c.Writer)
   145  }
   146  
   147  // GetProxyLocation responds with proxy connection location
   148  // swagger:operation GET /connection/proxy/location Connection getProxyLocation
   149  //
   150  //	---
   151  //	summary: Returns proxy connection location
   152  //	description: Returns proxy connection locations
   153  //	responses:
   154  //	  200:
   155  //	    description: Proxy connection locations
   156  //	    schema:
   157  //	      "$ref": "#/definitions/LocationDTO"
   158  //	  503:
   159  //	    description: Service unavailable
   160  //	    schema:
   161  //	      "$ref": "#/definitions/APIError"
   162  func (le *ConnectionLocationEndpoint) GetProxyLocation(c *gin.Context) {
   163  	p, _ := strconv.Atoi(c.Query("port"))
   164  	currentLocation, err := le.locationResolver.DetectProxyLocation(p)
   165  	if err != nil {
   166  		c.Error(apierror.ServiceUnavailable())
   167  		return
   168  	}
   169  	utils.WriteAsJSON(locationToRes(currentLocation), c.Writer)
   170  }
   171  
   172  // GetOriginLocation responds with original locations
   173  // swagger:operation GET /location Location getOriginLocation
   174  //
   175  //	---
   176  //	summary: Returns original location
   177  //	description: Returns original locations
   178  //	responses:
   179  //	  200:
   180  //	    description: Original locations
   181  //	    schema:
   182  //	      "$ref": "#/definitions/LocationDTO"
   183  func (le *ConnectionLocationEndpoint) GetOriginLocation(c *gin.Context) {
   184  	originLocation := le.locationOriginResolver.GetOrigin()
   185  
   186  	utils.WriteAsJSON(locationToRes(originLocation), c.Writer)
   187  }
   188  
   189  // AddRoutesForConnectionLocation adds connection location routes to given router
   190  func AddRoutesForConnectionLocation(
   191  	ipResolver ip.Resolver,
   192  	locationResolver location.Resolver,
   193  	locationOriginResolver location.OriginResolver,
   194  ) func(*gin.Engine) error {
   195  	connectionLocationEndpoint := NewConnectionLocationEndpoint(ipResolver, locationResolver, locationOriginResolver)
   196  	return func(e *gin.Engine) error {
   197  		connGroup := e.Group("/connection")
   198  		{
   199  			connGroup.GET("/ip", connectionLocationEndpoint.GetConnectionIP)
   200  			connGroup.GET("/proxy/ip", connectionLocationEndpoint.GetProxyIP)
   201  			connGroup.GET("/proxy/location", connectionLocationEndpoint.GetProxyLocation)
   202  			connGroup.GET("/location", connectionLocationEndpoint.GetConnectionLocation)
   203  		}
   204  
   205  		e.GET("/location", connectionLocationEndpoint.GetOriginLocation)
   206  		return nil
   207  	}
   208  }