options.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. package goSam
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. )
  7. //Option is a client Option
  8. type Option func(*Client) error
  9. //SetAddr sets a clients's address in the form host:port or host, port
  10. func SetAddr(s ...string) func(*Client) error {
  11. return func(c *Client) error {
  12. if len(s) == 1 {
  13. split := strings.SplitN(s[0], ":", 2)
  14. if len(split) == 2 {
  15. if i, err := strconv.Atoi(split[1]); err == nil {
  16. if i < 65536 {
  17. c.host = split[0]
  18. c.port = split[1]
  19. return nil
  20. }
  21. return fmt.Errorf("Invalid port")
  22. }
  23. return fmt.Errorf("Invalid port; non-number")
  24. }
  25. return fmt.Errorf("Invalid address; use host:port %s", split)
  26. } else if len(s) == 2 {
  27. if i, err := strconv.Atoi(s[1]); err == nil {
  28. if i < 65536 {
  29. c.host = s[0]
  30. c.port = s[1]
  31. return nil
  32. }
  33. return fmt.Errorf("Invalid port")
  34. }
  35. return fmt.Errorf("Invalid port; non-number")
  36. } else {
  37. return fmt.Errorf("Invalid address")
  38. }
  39. }
  40. }
  41. //SetAddrMixed sets a clients's address in the form host, port(int)
  42. func SetAddrMixed(s string, i int) func(*Client) error {
  43. return func(c *Client) error {
  44. if i < 65536 && i > 0 {
  45. c.host = s
  46. c.port = strconv.Itoa(i)
  47. return nil
  48. }
  49. return fmt.Errorf("Invalid port")
  50. }
  51. }
  52. //SetHost sets the host of the client's SAM bridge
  53. func SetHost(s string) func(*Client) error {
  54. return func(c *Client) error {
  55. c.host = s
  56. return nil
  57. }
  58. }
  59. //SetPort sets the port of the client's SAM bridge using a string
  60. func SetPort(s string) func(*Client) error {
  61. return func(c *Client) error {
  62. port, err := strconv.Atoi(s)
  63. if err != nil {
  64. return fmt.Errorf("Invalid port; non-number")
  65. }
  66. if port < 65536 && port > -1 {
  67. c.port = s
  68. return nil
  69. }
  70. return fmt.Errorf("Invalid port")
  71. }
  72. }
  73. //SetPortInt sets the port of the client's SAM bridge using a string
  74. func SetPortInt(i int) func(*Client) error {
  75. return func(c *Client) error {
  76. if i < 65536 && i > -1 {
  77. c.port = strconv.Itoa(i)
  78. return nil
  79. }
  80. return fmt.Errorf("Invalid port")
  81. }
  82. }
  83. //SetDebug enables debugging messages
  84. func SetDebug(b bool) func(*Client) error {
  85. return func(c *Client) error {
  86. c.debug = b
  87. return nil
  88. }
  89. }
  90. //SetInLength sets the number of hops inbound
  91. func SetInLength(u uint) func(*Client) error {
  92. return func(c *Client) error {
  93. if u < 7 {
  94. c.inLength = u
  95. return nil
  96. }
  97. return fmt.Errorf("Invalid inbound tunnel length")
  98. }
  99. }
  100. //SetOutLength sets the number of hops outbound
  101. func SetOutLength(u uint) func(*Client) error {
  102. return func(c *Client) error {
  103. if u < 7 {
  104. c.outLength = u
  105. return nil
  106. }
  107. return fmt.Errorf("Invalid outbound tunnel length")
  108. }
  109. }
  110. //SetInVariance sets the variance of a number of hops inbound
  111. func SetInVariance(i int) func(*Client) error {
  112. return func(c *Client) error {
  113. if i < 7 && i > -7 {
  114. c.inVariance = i
  115. return nil
  116. }
  117. return fmt.Errorf("Invalid inbound tunnel length")
  118. }
  119. }
  120. //SetOutVariance sets the variance of a number of hops outbound
  121. func SetOutVariance(i int) func(*Client) error {
  122. return func(c *Client) error {
  123. if i < 7 && i > -7 {
  124. c.outVariance = i
  125. return nil
  126. }
  127. return fmt.Errorf("Invalid outbound tunnel variance")
  128. }
  129. }
  130. //SetInQuantity sets the inbound tunnel quantity
  131. func SetInQuantity(u uint) func(*Client) error {
  132. return func(c *Client) error {
  133. if u <= 16 {
  134. c.inQuantity = u
  135. return nil
  136. }
  137. return fmt.Errorf("Invalid inbound tunnel quantity")
  138. }
  139. }
  140. //SetOutQuantity sets the outbound tunnel quantity
  141. func SetOutQuantity(u uint) func(*Client) error {
  142. return func(c *Client) error {
  143. if u <= 16 {
  144. c.outQuantity = u
  145. return nil
  146. }
  147. return fmt.Errorf("Invalid outbound tunnel quantity")
  148. }
  149. }
  150. //SetInBackups sets the inbound tunnel backups
  151. func SetInBackups(u uint) func(*Client) error {
  152. return func(c *Client) error {
  153. if u < 6 {
  154. c.inBackups = u
  155. return nil
  156. }
  157. return fmt.Errorf("Invalid inbound tunnel backup quantity")
  158. }
  159. }
  160. //SetOutBackups sets the inbound tunnel backups
  161. func SetOutBackups(u uint) func(*Client) error {
  162. return func(c *Client) error {
  163. if u < 6 {
  164. c.outBackups = u
  165. return nil
  166. }
  167. return fmt.Errorf("Invalid outbound tunnel backup quantity")
  168. }
  169. }
  170. //SetUnpublished tells the router to not publish the client leaseset
  171. func SetUnpublished(b bool) func(*Client) error {
  172. return func(c *Client) error {
  173. c.dontPublishLease = b
  174. return nil
  175. }
  176. }
  177. //SetEncrypt tells the router to use an encrypted leaseset
  178. func SetEncrypt(b bool) func(*Client) error {
  179. return func(c *Client) error {
  180. c.encryptLease = b
  181. return nil
  182. }
  183. }
  184. //SetReduceIdle tells the router to use an encrypted leaseset
  185. func SetReduceIdle(b bool) func(*Client) error {
  186. return func(c *Client) error {
  187. c.reduceIdle = b
  188. return nil
  189. }
  190. }
  191. //SetReduceIdleTime sets the inbound tunnel backups
  192. func SetReduceIdleTime(u uint) func(*Client) error {
  193. return func(c *Client) error {
  194. if u > 300000 {
  195. c.reduceIdleTime = u
  196. return nil
  197. }
  198. return fmt.Errorf("Invalid reduce idle time %v", u)
  199. }
  200. }
  201. //SetReduceIdleQuantity sets the inbound tunnel backups
  202. func SetReduceIdleQuantity(u uint) func(*Client) error {
  203. return func(c *Client) error {
  204. if u < 5 {
  205. c.reduceIdleQuantity = u
  206. return nil
  207. }
  208. return fmt.Errorf("Invalid reduced tunnel quantity %v", u)
  209. }
  210. }
  211. //SetCloseIdle tells the router to use an encrypted leaseset
  212. func SetCloseIdle(b bool) func(*Client) error {
  213. return func(c *Client) error {
  214. c.closeIdle = b
  215. return nil
  216. }
  217. }
  218. //SetCloseIdleTime sets the inbound tunnel backups
  219. func SetCloseIdleTime(u uint) func(*Client) error {
  220. return func(c *Client) error {
  221. if u > 300000 {
  222. c.closeIdleTime = u
  223. return nil
  224. }
  225. return fmt.Errorf("Invalid close idle time %v", u)
  226. }
  227. }
  228. //return the inbound length as a string.
  229. func (c *Client) inlength() string {
  230. return fmt.Sprintf("inbound.length=%d", c.inLength)
  231. }
  232. //return the outbound length as a string.
  233. func (c *Client) outlength() string {
  234. return fmt.Sprintf("outbound.length=%d", c.outLength)
  235. }
  236. //return the inbound length variance as a string.
  237. func (c *Client) invariance() string {
  238. return fmt.Sprintf("inbound.lengthVariance=%d", c.inVariance)
  239. }
  240. //return the outbound length variance as a string.
  241. func (c *Client) outvariance() string {
  242. return fmt.Sprintf("outbound.lengthVariance=%d", c.outVariance)
  243. }
  244. //return the inbound tunnel quantity as a string.
  245. func (c *Client) inquantity() string {
  246. return fmt.Sprintf("inbound.quantity=%d", c.inQuantity)
  247. }
  248. //return the outbound tunnel quantity as a string.
  249. func (c *Client) outquantity() string {
  250. return fmt.Sprintf("outbound.quantity=%d", c.outQuantity)
  251. }
  252. //return the inbound tunnel quantity as a string.
  253. func (c *Client) inbackups() string {
  254. return fmt.Sprintf("inbound.backupQuantity=%d", c.inQuantity)
  255. }
  256. //return the outbound tunnel quantity as a string.
  257. func (c *Client) outbackups() string {
  258. return fmt.Sprintf("outbound.backupQuantity=%d", c.outQuantity)
  259. }
  260. func (c *Client) encryptlease() string {
  261. if c.encryptLease {
  262. return "i2cp.encryptLeaseSet=true"
  263. }
  264. return "i2cp.encryptLeaseSet=false"
  265. }
  266. func (c *Client) dontpublishlease() string {
  267. if c.dontPublishLease {
  268. return "i2cp.dontPublishLeaseSet=true"
  269. }
  270. return "i2cp.dontPublishLeaseSet=false"
  271. }
  272. func (c *Client) closeonidle() string {
  273. if c.closeIdle {
  274. return "i2cp.closeOnIdle=true"
  275. }
  276. return "i2cp.closeOnIdle=false"
  277. }
  278. func (c *Client) closeidletime() string {
  279. return fmt.Sprintf("i2cp.closeIdleTime=%d", c.closeIdleTime)
  280. }
  281. func (c *Client) reduceonidle() string {
  282. if c.reduceIdle {
  283. return "i2cp.reduceOnIdle=true"
  284. }
  285. return "i2cp.reduceOnIdle=false"
  286. }
  287. func (c *Client) reduceidletime() string {
  288. return fmt.Sprintf("i2cp.reduceIdleTime=%d", c.reduceIdleTime)
  289. }
  290. func (c *Client) reduceidlecount() string {
  291. return fmt.Sprintf("i2cp.reduceIdleQuantity=%d", c.reduceIdleQuantity)
  292. }
  293. //return all options as string array ready for passing to sendcmd
  294. func (c *Client) allOptions() []string {
  295. return []string{
  296. c.inlength(),
  297. c.outlength(),
  298. c.invariance(),
  299. c.outvariance(),
  300. c.inquantity(),
  301. c.outquantity(),
  302. c.inbackups(),
  303. c.outbackups(),
  304. c.dontpublishlease(),
  305. c.encryptlease(),
  306. c.reduceonidle(),
  307. c.reduceidletime(),
  308. c.reduceidlecount(),
  309. c.closeonidle(),
  310. c.closeidletime(),
  311. }
  312. }