api.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (c) 2018, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package common
  20. // APIParameters is a set of API parameter values, typically received
  21. // from a Psiphon client and used/logged by the Psiphon server. The
  22. // values are of varying types: strings, ints, arrays, structs, etc.
  23. type APIParameters map[string]interface{}
  24. // Add copies API parameters from b to a, skipping parameters which already
  25. // exist, regardless of value, in a.
  26. func (a APIParameters) Add(b APIParameters) {
  27. for name, value := range b {
  28. _, ok := a[name]
  29. if !ok {
  30. a[name] = value
  31. }
  32. }
  33. }
  34. // APIParameterValidator is a function that validates API parameters
  35. // for a particular request or context.
  36. type APIParameterValidator func(APIParameters) error
  37. // GeoIPData is type-compatible with psiphon/server.GeoIPData.
  38. type GeoIPData struct {
  39. Country string
  40. City string
  41. ISP string
  42. ASN string
  43. ASO string
  44. }
  45. // APIParameterLogFieldFormatter is a function that returns formatted
  46. // LogFields containing the given GeoIPData and APIParameters. An optional
  47. // log field name prefix string, when specified, should be applied to the
  48. // output LogFields names. When GeoIPData is the zero value, it should be
  49. // omitted from LogFields.
  50. type APIParameterLogFieldFormatter func(string, GeoIPData, APIParameters) LogFields