├── .gitignore ├── LICENSE ├── README.md ├── accept.go ├── client.go ├── client_test.go ├── dial.go ├── example └── httpTest.go ├── go.mod ├── go.sum ├── naming.go ├── naming_test.go ├── options.go ├── options_test.go ├── replyParser.go ├── replyParser_test.go ├── sessions.go └── stream.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | goSam 2 | ===== 3 | 4 | A go library for using the [I2P](https://geti2p.net/en/) Simple Anonymous Messaging ([SAM version 3.0](https://geti2p.net/en/docs/api/samv3)) bridge 5 | 6 | This is in an **early development stage**. I would love to hear about any issues or ideas for improvement. 7 | 8 | ## Installation 9 | ``` 10 | go get github.com/cryptix/goSam 11 | ``` 12 | 13 | ## Using it for HTTP Transport 14 | 15 | I implemented `Client.Dial` like `net.Dial` so you can use go's library packages like http. 16 | 17 | ```go 18 | package main 19 | 20 | import ( 21 | "io" 22 | "log" 23 | "net/http" 24 | "os" 25 | 26 | "github.com/cryptix/goSam" 27 | ) 28 | 29 | func main() { 30 | // create a default sam client 31 | sam, err := goSam.NewDefaultClient() 32 | checkErr(err) 33 | 34 | log.Println("Client Created") 35 | 36 | // create a transport that uses SAM to dial TCP Connections 37 | tr := &http.Transport{ 38 | Dial: sam.Dial, 39 | } 40 | 41 | // create a client using this transport 42 | client := &http.Client{Transport: tr} 43 | 44 | // send a get request 45 | resp, err := client.Get("http://stats.i2p/") 46 | checkErr(err) 47 | defer resp.Body.Close() 48 | 49 | log.Printf("Get returned %+v\n", resp) 50 | 51 | // create a file for the response 52 | file, err := os.Create("stats.html") 53 | checkErr(err) 54 | defer file.Close() 55 | 56 | // copy the response to the file 57 | _, err = io.Copy(file, resp.Body) 58 | checkErr(err) 59 | 60 | log.Println("Done.") 61 | } 62 | 63 | func checkErr(err error) { 64 | if err != nil { 65 | log.Fatal(err) 66 | } 67 | } 68 | ``` 69 | 70 | ### TODO 71 | 72 | * Implement `STREAM ACCEPT` and `STREAM FORWARD` 73 | * Implement datagrams (Repliable and Anon) -------------------------------------------------------------------------------- /accept.go: -------------------------------------------------------------------------------- 1 | package goSam 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | 7 | "github.com/cryptix/go/debug" 8 | ) 9 | 10 | // Accept creates a new Client and accepts a connection on it 11 | func (c *Client) Accept() (net.Conn, error) { 12 | id, newAddr, err := c.CreateStreamSession("") 13 | if err != nil { 14 | return nil, err 15 | } 16 | 17 | fmt.Println("NewAddr:", newAddr) 18 | 19 | newC, err := NewDefaultClient() 20 | if err != nil { 21 | return nil, err 22 | } 23 | 24 | if c.debug { 25 | newC.SamConn = debug.WrapConn(newC.SamConn) 26 | } 27 | 28 | resp, err := newC.StreamAccept(id) 29 | if err != nil { 30 | return nil, err 31 | } 32 | 33 | fmt.Println("Accept Resp:", resp) 34 | 35 | return newC.SamConn, nil 36 | } 37 | -------------------------------------------------------------------------------- /client.go: -------------------------------------------------------------------------------- 1 | package goSam 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "net" 7 | 8 | "github.com/cryptix/go/debug" 9 | ) 10 | 11 | // A Client represents a single Connection to the SAM bridge 12 | type Client struct { 13 | host string 14 | port string 15 | 16 | SamConn net.Conn 17 | rd *bufio.Reader 18 | 19 | inLength uint 20 | inVariance int 21 | inQuantity uint 22 | inBackups uint 23 | 24 | outLength uint 25 | outVariance int 26 | outQuantity uint 27 | outBackups uint 28 | 29 | dontPublishLease bool 30 | encryptLease bool 31 | 32 | reduceIdle bool 33 | reduceIdleTime uint 34 | reduceIdleQuantity uint 35 | 36 | closeIdle bool 37 | closeIdleTime uint 38 | 39 | debug bool 40 | } 41 | 42 | // NewDefaultClient creates a new client, connecting to the default host:port at localhost:7656 43 | func NewDefaultClient() (*Client, error) { 44 | return NewClient("localhost:7656") 45 | } 46 | 47 | // NewClient creates a new client, connecting to a specified port 48 | func NewClient(addr string) (*Client, error) { 49 | return NewClientFromOptions(SetAddr(addr)) 50 | } 51 | 52 | // NewClientFromOptions creates a new client, connecting to a specified port 53 | func NewClientFromOptions(opts ...func(*Client) error) (*Client, error) { 54 | var c Client 55 | c.host = "127.0.0.1" 56 | c.port = "7656" 57 | c.inLength = 3 58 | c.inVariance = 0 59 | c.inQuantity = 4 60 | c.inBackups = 2 61 | c.outLength = 3 62 | c.outVariance = 0 63 | c.outQuantity = 4 64 | c.outBackups = 2 65 | c.dontPublishLease = true 66 | c.encryptLease = false 67 | c.reduceIdle = false 68 | c.reduceIdleTime = 300000 69 | c.reduceIdleQuantity = 4 70 | c.closeIdle = true 71 | c.closeIdleTime = 600000 72 | c.debug = false 73 | for _, o := range opts { 74 | if err := o(&c); err != nil { 75 | return nil, err 76 | } 77 | } 78 | conn, err := net.Dial("tcp", c.samaddr()) 79 | if err != nil { 80 | return nil, err 81 | } 82 | if c.debug { 83 | conn = debug.WrapConn(conn) 84 | } 85 | c.SamConn = conn 86 | c.rd = bufio.NewReader(conn) 87 | return &c, c.hello() 88 | } 89 | 90 | //return the combined host:port of the SAM bridge 91 | func (c *Client) samaddr() string { 92 | return fmt.Sprintf("%s:%s", c.host, c.port) 93 | } 94 | 95 | // send the initial handshake command and check that the reply is ok 96 | func (c *Client) hello() error { 97 | r, err := c.sendCmd("HELLO VERSION MIN=3.0 MAX=3.2\n") 98 | if err != nil { 99 | return err 100 | } 101 | 102 | if r.Topic != "HELLO" { 103 | return fmt.Errorf("Unknown Reply: %+v\n", r) 104 | } 105 | if r.Pairs["RESULT"] != "OK" || !(r.Pairs["VERSION"] == "3.0" || r.Pairs["VERSION"] == "3.1" || r.Pairs["VERSION"] == "3.2") { 106 | return fmt.Errorf("Handshake did not succeed\nReply:%+v\n", r) 107 | } 108 | 109 | return nil 110 | } 111 | 112 | // helper to send one command and parse the reply by sam 113 | func (c *Client) sendCmd(str string, args ...interface{}) (*Reply, error) { 114 | if _, err := fmt.Fprintf(c.SamConn, str, args...); err != nil { 115 | return nil, err 116 | } 117 | 118 | line, err := c.rd.ReadString('\n') 119 | if err != nil { 120 | return nil, err 121 | } 122 | 123 | return parseReply(line) 124 | } 125 | 126 | // Close the underlying socket to SAM 127 | func (c *Client) Close() error { 128 | c.rd = nil 129 | return c.SamConn.Close() 130 | } 131 | -------------------------------------------------------------------------------- /client_test.go: -------------------------------------------------------------------------------- 1 | package goSam 2 | 3 | import "testing" 4 | 5 | var client *Client 6 | 7 | func setup(t *testing.T) { 8 | var err error 9 | 10 | // these tests expect a running SAM brige on this address 11 | client, err = NewClientFromOptions(SetDebug(true)) 12 | if err != nil { 13 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 14 | } 15 | 16 | } 17 | 18 | func teardown(t *testing.T) { 19 | if err := client.Close(); err != nil { 20 | t.Fatalf("client.Close() Error: %q\n", err) 21 | } 22 | } 23 | 24 | func TestClientHello(t *testing.T) { 25 | setup(t) 26 | teardown(t) 27 | } 28 | -------------------------------------------------------------------------------- /dial.go: -------------------------------------------------------------------------------- 1 | package goSam 2 | 3 | import ( 4 | "net" 5 | "strings" 6 | ) 7 | 8 | // Dial implements the net.Dial function and can be used for http.Transport 9 | func (c *Client) Dial(network, addr string) (net.Conn, error) { 10 | portIdx := strings.Index(addr, ":") 11 | if portIdx >= 0 { 12 | addr = addr[:portIdx] 13 | } 14 | addr, err := c.Lookup(addr) 15 | if err != nil { 16 | return nil, err 17 | } 18 | 19 | id, _, err := c.CreateStreamSession("") 20 | if err != nil { 21 | return nil, err 22 | } 23 | 24 | newC, err := NewClient(c.samaddr()) 25 | if err != nil { 26 | return nil, err 27 | } 28 | 29 | err = newC.StreamConnect(id, addr) 30 | if err != nil { 31 | return nil, err 32 | } 33 | 34 | return newC.SamConn, nil 35 | } 36 | -------------------------------------------------------------------------------- /example/httpTest.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "io" 5 | "log" 6 | "net/http" 7 | "os" 8 | 9 | "github.com/cryptix/goSam" 10 | ) 11 | 12 | func main() { 13 | //In order to enable debugging, pass the SetDebug(true) option. 14 | //sam, err := goSam.NewClientFromOptions(SetDebug(true)) 15 | 16 | // create a default sam client 17 | sam, err := goSam.NewDefaultClient() 18 | checkErr(err) 19 | 20 | log.Println("Client Created") 21 | 22 | // create a transport that uses SAM to dial TCP Connections 23 | tr := &http.Transport{ 24 | Dial: sam.Dial, 25 | } 26 | 27 | // create a client using this transport 28 | client := &http.Client{Transport: tr} 29 | 30 | // send a get request 31 | resp, err := client.Get("http://stats.i2p/") 32 | checkErr(err) 33 | defer resp.Body.Close() 34 | 35 | log.Printf("Get returned %+v\n", resp) 36 | 37 | // create a file for the response 38 | file, err := os.Create("stats.html") 39 | checkErr(err) 40 | defer file.Close() 41 | 42 | // copy the response to the file 43 | _, err = io.Copy(file, resp.Body) 44 | checkErr(err) 45 | 46 | log.Println("Done.") 47 | } 48 | 49 | func checkErr(err error) { 50 | if err != nil { 51 | log.Fatal(err) 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/cryptix/goSam 2 | 3 | require ( 4 | github.com/cryptix/go v1.3.1 5 | github.com/miolini/datacounter v0.0.0-20171104152933-fd4e42a1d5e0 // indirect 6 | ) 7 | 8 | go 1.13 9 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/cryptix/go v1.3.1 h1:I9opbROgEpldI0PwkMku0UY2DLFYgelZd9u0uaxmMgY= 2 | github.com/cryptix/go v1.3.1/go.mod h1:mFQotm9rTzptzvNjJM+1vSIDa/rVOVqMu0889GIXg70= 3 | github.com/go-kit/kit v0.6.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= 4 | github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= 5 | github.com/go-stack/stack v1.7.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= 6 | github.com/miolini/datacounter v0.0.0-20171104152933-fd4e42a1d5e0 h1:clkDYGefEWUCwyCrwYn900sOaVGDpinPJgD0W6ebEjs= 7 | github.com/miolini/datacounter v0.0.0-20171104152933-fd4e42a1d5e0/go.mod h1:P6fDJzlxN+cWYR09KbE9/ta+Y6JofX9tAUhJpWkWPaM= 8 | github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 9 | -------------------------------------------------------------------------------- /naming.go: -------------------------------------------------------------------------------- 1 | package goSam 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // Lookup askes SAM for the internal i2p address from name 9 | func (c *Client) Lookup(name string) (string, error) { 10 | r, err := c.sendCmd("NAMING LOOKUP NAME=%s\n", name) 11 | if err != nil { 12 | return "", nil 13 | } 14 | 15 | // TODO: move check into sendCmd() 16 | if r.Topic != "NAMING" || r.Type != "REPLY" { 17 | return "", fmt.Errorf("Unknown Reply: %+v\n", r) 18 | } 19 | 20 | result := r.Pairs["RESULT"] 21 | if result != "OK" { 22 | return "", ReplyError{result, r} 23 | } 24 | 25 | if r.Pairs["NAME"] != name { 26 | // somehow different on i2pd 27 | if r.Pairs["NAME"] != "ME" { 28 | return "", fmt.Errorf("Lookup() Replyed to another name.\nWanted:%s\nGot: %+v\n", name, r) 29 | } 30 | fmt.Fprintln(os.Stderr, "WARNING: Lookup() Replyed to another name. assuming i2pd c++ fluke") 31 | } 32 | 33 | return r.Pairs["VALUE"], nil 34 | } 35 | -------------------------------------------------------------------------------- /naming_test.go: -------------------------------------------------------------------------------- 1 | package goSam 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func TestClientLookupInvalid(t *testing.T) { 9 | var err error 10 | 11 | setup(t) 12 | defer teardown(t) 13 | 14 | addr, err := client.Lookup(`!(@#)`) 15 | if addr != "" || err == nil { 16 | t.Error("client.Lookup() should throw an error.") 17 | } 18 | 19 | repErr, ok := err.(ReplyError) 20 | if !ok { 21 | t.Fatalf("client.Lookup() should return a ReplyError") 22 | } 23 | if repErr.Result != ResultInvalidKey { 24 | t.Errorf("client.Lookup() should throw an ResultKeyNotFound error.\nGot:%+v\n", repErr) 25 | } 26 | } 27 | 28 | func ExampleClient_Lookup() { 29 | client, err := NewDefaultClient() 30 | if err != nil { 31 | fmt.Printf("NewDefaultClient() should not throw an error.\n%s\n", err) 32 | return 33 | } 34 | 35 | addr, err := client.Lookup("zzz.i2p") 36 | if err != nil { 37 | fmt.Printf("client.Lookup() should not throw an error.\n%s\n", err) 38 | return 39 | } 40 | 41 | fmt.Println("Address of zzz.i2p:") 42 | // Addresses change all the time 43 | fmt.Println(addr) 44 | 45 | // Output: 46 | //Address of zzz.i2p: 47 | //GKapJ8koUcBj~jmQzHsTYxDg2tpfWj0xjQTzd8BhfC9c3OS5fwPBNajgF-eOD6eCjFTqTlorlh7Hnd8kXj1qblUGXT-tDoR9~YV8dmXl51cJn9MVTRrEqRWSJVXbUUz9t5Po6Xa247Vr0sJn27R4KoKP8QVj1GuH6dB3b6wTPbOamC3dkO18vkQkfZWUdRMDXk0d8AdjB0E0864nOT~J9Fpnd2pQE5uoFT6P0DqtQR2jsFvf9ME61aqLvKPPWpkgdn4z6Zkm-NJOcDz2Nv8Si7hli94E9SghMYRsdjU-knObKvxiagn84FIwcOpepxuG~kFXdD5NfsH0v6Uri3usE3uSzpWS0EHmrlfoLr5uGGd9ZHwwCIcgfOATaPRMUEQxiK9q48PS0V3EXXO4-YLT0vIfk4xO~XqZpn8~PW1kFe2mQMHd7oO89yCk-3yizRG3UyFtI7-mO~eCI6-m1spYoigStgoupnC3G85gJkqEjMm49gUjbhfWKWI-6NwTj0ZnAAAA 48 | } 49 | -------------------------------------------------------------------------------- /options.go: -------------------------------------------------------------------------------- 1 | package goSam 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | "strings" 7 | ) 8 | 9 | //Option is a client Option 10 | type Option func(*Client) error 11 | 12 | //SetAddr sets a clients's address in the form host:port or host, port 13 | func SetAddr(s ...string) func(*Client) error { 14 | return func(c *Client) error { 15 | if len(s) == 1 { 16 | split := strings.SplitN(s[0], ":", 2) 17 | if len(split) == 2 { 18 | if i, err := strconv.Atoi(split[1]); err == nil { 19 | if i < 65536 { 20 | c.host = split[0] 21 | c.port = split[1] 22 | return nil 23 | } 24 | return fmt.Errorf("Invalid port") 25 | } 26 | return fmt.Errorf("Invalid port; non-number") 27 | } 28 | return fmt.Errorf("Invalid address; use host:port %s", split) 29 | } else if len(s) == 2 { 30 | if i, err := strconv.Atoi(s[1]); err == nil { 31 | if i < 65536 { 32 | c.host = s[0] 33 | c.port = s[1] 34 | return nil 35 | } 36 | return fmt.Errorf("Invalid port") 37 | } 38 | return fmt.Errorf("Invalid port; non-number") 39 | } else { 40 | return fmt.Errorf("Invalid address") 41 | } 42 | } 43 | } 44 | 45 | //SetAddrMixed sets a clients's address in the form host, port(int) 46 | func SetAddrMixed(s string, i int) func(*Client) error { 47 | return func(c *Client) error { 48 | if i < 65536 && i > 0 { 49 | c.host = s 50 | c.port = strconv.Itoa(i) 51 | return nil 52 | } 53 | return fmt.Errorf("Invalid port") 54 | } 55 | } 56 | 57 | //SetHost sets the host of the client's SAM bridge 58 | func SetHost(s string) func(*Client) error { 59 | return func(c *Client) error { 60 | c.host = s 61 | return nil 62 | } 63 | } 64 | 65 | //SetPort sets the port of the client's SAM bridge using a string 66 | func SetPort(s string) func(*Client) error { 67 | return func(c *Client) error { 68 | port, err := strconv.Atoi(s) 69 | if err != nil { 70 | return fmt.Errorf("Invalid port; non-number") 71 | } 72 | if port < 65536 && port > -1 { 73 | c.port = s 74 | return nil 75 | } 76 | return fmt.Errorf("Invalid port") 77 | } 78 | } 79 | 80 | //SetPortInt sets the port of the client's SAM bridge using a string 81 | func SetPortInt(i int) func(*Client) error { 82 | return func(c *Client) error { 83 | if i < 65536 && i > -1 { 84 | c.port = strconv.Itoa(i) 85 | return nil 86 | } 87 | return fmt.Errorf("Invalid port") 88 | } 89 | } 90 | 91 | //SetDebug enables debugging messages 92 | func SetDebug(b bool) func(*Client) error { 93 | return func(c *Client) error { 94 | c.debug = b 95 | return nil 96 | } 97 | } 98 | 99 | //SetInLength sets the number of hops inbound 100 | func SetInLength(u uint) func(*Client) error { 101 | return func(c *Client) error { 102 | if u < 7 { 103 | c.inLength = u 104 | return nil 105 | } 106 | return fmt.Errorf("Invalid inbound tunnel length") 107 | } 108 | } 109 | 110 | //SetOutLength sets the number of hops outbound 111 | func SetOutLength(u uint) func(*Client) error { 112 | return func(c *Client) error { 113 | if u < 7 { 114 | c.outLength = u 115 | return nil 116 | } 117 | return fmt.Errorf("Invalid outbound tunnel length") 118 | } 119 | } 120 | 121 | //SetInVariance sets the variance of a number of hops inbound 122 | func SetInVariance(i int) func(*Client) error { 123 | return func(c *Client) error { 124 | if i < 7 && i > -7 { 125 | c.inVariance = i 126 | return nil 127 | } 128 | return fmt.Errorf("Invalid inbound tunnel length") 129 | } 130 | } 131 | 132 | //SetOutVariance sets the variance of a number of hops outbound 133 | func SetOutVariance(i int) func(*Client) error { 134 | return func(c *Client) error { 135 | if i < 7 && i > -7 { 136 | c.outVariance = i 137 | return nil 138 | } 139 | return fmt.Errorf("Invalid outbound tunnel variance") 140 | } 141 | } 142 | 143 | //SetInQuantity sets the inbound tunnel quantity 144 | func SetInQuantity(u uint) func(*Client) error { 145 | return func(c *Client) error { 146 | if u <= 16 { 147 | c.inQuantity = u 148 | return nil 149 | } 150 | return fmt.Errorf("Invalid inbound tunnel quantity") 151 | } 152 | } 153 | 154 | //SetOutQuantity sets the outbound tunnel quantity 155 | func SetOutQuantity(u uint) func(*Client) error { 156 | return func(c *Client) error { 157 | if u <= 16 { 158 | c.outQuantity = u 159 | return nil 160 | } 161 | return fmt.Errorf("Invalid outbound tunnel quantity") 162 | } 163 | } 164 | 165 | //SetInBackups sets the inbound tunnel backups 166 | func SetInBackups(u uint) func(*Client) error { 167 | return func(c *Client) error { 168 | if u < 6 { 169 | c.inBackups = u 170 | return nil 171 | } 172 | return fmt.Errorf("Invalid inbound tunnel backup quantity") 173 | } 174 | } 175 | 176 | //SetOutBackups sets the inbound tunnel backups 177 | func SetOutBackups(u uint) func(*Client) error { 178 | return func(c *Client) error { 179 | if u < 6 { 180 | c.outBackups = u 181 | return nil 182 | } 183 | return fmt.Errorf("Invalid outbound tunnel backup quantity") 184 | } 185 | } 186 | 187 | //SetUnpublished tells the router to not publish the client leaseset 188 | func SetUnpublished(b bool) func(*Client) error { 189 | return func(c *Client) error { 190 | c.dontPublishLease = b 191 | return nil 192 | } 193 | } 194 | 195 | //SetEncrypt tells the router to use an encrypted leaseset 196 | func SetEncrypt(b bool) func(*Client) error { 197 | return func(c *Client) error { 198 | c.encryptLease = b 199 | return nil 200 | } 201 | } 202 | 203 | //SetReduceIdle tells the router to use an encrypted leaseset 204 | func SetReduceIdle(b bool) func(*Client) error { 205 | return func(c *Client) error { 206 | c.reduceIdle = b 207 | return nil 208 | } 209 | } 210 | 211 | //SetReduceIdleTime sets the inbound tunnel backups 212 | func SetReduceIdleTime(u uint) func(*Client) error { 213 | return func(c *Client) error { 214 | if u > 300000 { 215 | c.reduceIdleTime = u 216 | return nil 217 | } 218 | return fmt.Errorf("Invalid reduce idle time %v", u) 219 | } 220 | } 221 | 222 | //SetReduceIdleQuantity sets the inbound tunnel backups 223 | func SetReduceIdleQuantity(u uint) func(*Client) error { 224 | return func(c *Client) error { 225 | if u < 5 { 226 | c.reduceIdleQuantity = u 227 | return nil 228 | } 229 | return fmt.Errorf("Invalid reduced tunnel quantity %v", u) 230 | } 231 | } 232 | 233 | //SetCloseIdle tells the router to use an encrypted leaseset 234 | func SetCloseIdle(b bool) func(*Client) error { 235 | return func(c *Client) error { 236 | c.closeIdle = b 237 | return nil 238 | } 239 | } 240 | 241 | //SetCloseIdleTime sets the inbound tunnel backups 242 | func SetCloseIdleTime(u uint) func(*Client) error { 243 | return func(c *Client) error { 244 | if u > 300000 { 245 | c.closeIdleTime = u 246 | return nil 247 | } 248 | return fmt.Errorf("Invalid close idle time %v", u) 249 | } 250 | } 251 | 252 | //return the inbound length as a string. 253 | func (c *Client) inlength() string { 254 | return fmt.Sprintf("inbound.length=%d", c.inLength) 255 | } 256 | 257 | //return the outbound length as a string. 258 | func (c *Client) outlength() string { 259 | return fmt.Sprintf("outbound.length=%d", c.outLength) 260 | } 261 | 262 | //return the inbound length variance as a string. 263 | func (c *Client) invariance() string { 264 | return fmt.Sprintf("inbound.lengthVariance=%d", c.inVariance) 265 | } 266 | 267 | //return the outbound length variance as a string. 268 | func (c *Client) outvariance() string { 269 | return fmt.Sprintf("outbound.lengthVariance=%d", c.outVariance) 270 | } 271 | 272 | //return the inbound tunnel quantity as a string. 273 | func (c *Client) inquantity() string { 274 | return fmt.Sprintf("inbound.quantity=%d", c.inQuantity) 275 | } 276 | 277 | //return the outbound tunnel quantity as a string. 278 | func (c *Client) outquantity() string { 279 | return fmt.Sprintf("outbound.quantity=%d", c.outQuantity) 280 | } 281 | 282 | //return the inbound tunnel quantity as a string. 283 | func (c *Client) inbackups() string { 284 | return fmt.Sprintf("inbound.backupQuantity=%d", c.inQuantity) 285 | } 286 | 287 | //return the outbound tunnel quantity as a string. 288 | func (c *Client) outbackups() string { 289 | return fmt.Sprintf("outbound.backupQuantity=%d", c.outQuantity) 290 | } 291 | 292 | func (c *Client) encryptlease() string { 293 | if c.encryptLease { 294 | return "i2cp.encryptLeaseSet=true" 295 | } 296 | return "i2cp.encryptLeaseSet=false" 297 | } 298 | 299 | func (c *Client) dontpublishlease() string { 300 | if c.dontPublishLease { 301 | return "i2cp.dontPublishLeaseSet=true" 302 | } 303 | return "i2cp.dontPublishLeaseSet=false" 304 | } 305 | 306 | func (c *Client) closeonidle() string { 307 | if c.closeIdle { 308 | return "i2cp.closeOnIdle=true" 309 | } 310 | return "i2cp.closeOnIdle=false" 311 | } 312 | 313 | func (c *Client) closeidletime() string { 314 | return fmt.Sprintf("i2cp.closeIdleTime=%d", c.closeIdleTime) 315 | } 316 | 317 | func (c *Client) reduceonidle() string { 318 | if c.reduceIdle { 319 | return "i2cp.reduceOnIdle=true" 320 | } 321 | return "i2cp.reduceOnIdle=false" 322 | } 323 | 324 | func (c *Client) reduceidletime() string { 325 | return fmt.Sprintf("i2cp.reduceIdleTime=%d", c.reduceIdleTime) 326 | } 327 | 328 | func (c *Client) reduceidlecount() string { 329 | return fmt.Sprintf("i2cp.reduceIdleQuantity=%d", c.reduceIdleQuantity) 330 | } 331 | 332 | //return all options as string array ready for passing to sendcmd 333 | func (c *Client) allOptions() []string { 334 | return []string{ 335 | c.inlength(), 336 | c.outlength(), 337 | c.invariance(), 338 | c.outvariance(), 339 | c.inquantity(), 340 | c.outquantity(), 341 | c.inbackups(), 342 | c.outbackups(), 343 | c.dontpublishlease(), 344 | c.encryptlease(), 345 | c.reduceonidle(), 346 | c.reduceidletime(), 347 | c.reduceidlecount(), 348 | c.closeonidle(), 349 | c.closeidletime(), 350 | } 351 | } 352 | -------------------------------------------------------------------------------- /options_test.go: -------------------------------------------------------------------------------- 1 | package goSam 2 | 3 | import "testing" 4 | 5 | func TestOptionAddrString(t *testing.T) { 6 | client, err := NewClientFromOptions(SetAddr("127.0.0.1:7656"), SetDebug(true)) 7 | if err != nil { 8 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 9 | } 10 | if err := client.Close(); err != nil { 11 | t.Fatalf("client.Close() Error: %q\n", err) 12 | } 13 | } 14 | 15 | func TestOptionAddrStringLh(t *testing.T) { 16 | client, err := NewClientFromOptions(SetAddr("localhost:7656"), SetDebug(true)) 17 | if err != nil { 18 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 19 | } 20 | if err := client.Close(); err != nil { 21 | t.Fatalf("client.Close() Error: %q\n", err) 22 | } 23 | } 24 | 25 | func TestOptionAddrSlice(t *testing.T) { 26 | client, err := NewClientFromOptions(SetAddr("127.0.0.1", "7656"), SetDebug(true)) 27 | if err != nil { 28 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 29 | } 30 | if err := client.Close(); err != nil { 31 | t.Fatalf("client.Close() Error: %q\n", err) 32 | } 33 | } 34 | 35 | func TestOptionAddrMixedSlice(t *testing.T) { 36 | client, err := NewClientFromOptions(SetAddrMixed("127.0.0.1", 7656), SetDebug(true)) 37 | if err != nil { 38 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 39 | } 40 | if err := client.Close(); err != nil { 41 | t.Fatalf("client.Close() Error: %q\n", err) 42 | } 43 | } 44 | 45 | func TestOptionHost(t *testing.T) { 46 | client, err := NewClientFromOptions(SetHost("127.0.0.1"), SetDebug(true)) 47 | if err != nil { 48 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 49 | } 50 | if err := client.Close(); err != nil { 51 | t.Fatalf("client.Close() Error: %q\n", err) 52 | } 53 | } 54 | 55 | func TestOptionPort(t *testing.T) { 56 | client, err := NewClientFromOptions(SetPort("7656"), SetDebug(true)) 57 | if err != nil { 58 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 59 | } 60 | if err := client.Close(); err != nil { 61 | t.Fatalf("client.Close() Error: %q\n", err) 62 | } 63 | } 64 | 65 | func TestOptionPortInt(t *testing.T) { 66 | client, err := NewClientFromOptions(SetPortInt(7656), SetDebug(true)) 67 | if err != nil { 68 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 69 | } 70 | if err := client.Close(); err != nil { 71 | t.Fatalf("client.Close() Error: %q\n", err) 72 | } 73 | } 74 | 75 | func TestOptionDebug(t *testing.T) { 76 | client, err := NewClientFromOptions(SetDebug(true)) 77 | if err != nil { 78 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 79 | } 80 | if err := client.Close(); err != nil { 81 | t.Fatalf("client.Close() Error: %q\n", err) 82 | } 83 | } 84 | 85 | func TestOptionInLength(t *testing.T) { 86 | client, err := NewClientFromOptions(SetInLength(3), SetDebug(true)) 87 | client.inlength() 88 | if err != nil { 89 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 90 | } 91 | if err := client.Close(); err != nil { 92 | t.Fatalf("client.Close() Error: %q\n", err) 93 | } 94 | } 95 | 96 | func TestOptionOutLength(t *testing.T) { 97 | client, err := NewClientFromOptions(SetInLength(3), SetDebug(true)) 98 | client.outlength() 99 | if err != nil { 100 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 101 | } 102 | if err := client.Close(); err != nil { 103 | t.Fatalf("client.Close() Error: %q\n", err) 104 | } 105 | } 106 | 107 | func TestOptionInVariance(t *testing.T) { 108 | client, err := NewClientFromOptions(SetInVariance(1), SetDebug(true)) 109 | client.invariance() 110 | if err != nil { 111 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 112 | } 113 | if err := client.Close(); err != nil { 114 | t.Fatalf("client.Close() Error: %q\n", err) 115 | } 116 | } 117 | 118 | func TestOptionOutVariance(t *testing.T) { 119 | client, err := NewClientFromOptions(SetOutVariance(1), SetDebug(true)) 120 | client.outvariance() 121 | if err != nil { 122 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 123 | } 124 | if err := client.Close(); err != nil { 125 | t.Fatalf("client.Close() Error: %q\n", err) 126 | } 127 | } 128 | 129 | func TestOptionInQuantity(t *testing.T) { 130 | client, err := NewClientFromOptions(SetInQuantity(6), SetDebug(true)) 131 | client.inquantity() 132 | if err != nil { 133 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 134 | } 135 | if err := client.Close(); err != nil { 136 | t.Fatalf("client.Close() Error: %q\n", err) 137 | } 138 | } 139 | 140 | func TestOptionOutQuantity(t *testing.T) { 141 | client, err := NewClientFromOptions(SetOutQuantity(6), SetDebug(true)) 142 | client.outquantity() 143 | if err != nil { 144 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 145 | } 146 | if err := client.Close(); err != nil { 147 | t.Fatalf("client.Close() Error: %q\n", err) 148 | } 149 | } 150 | 151 | func TestOptionInBackups(t *testing.T) { 152 | client, err := NewClientFromOptions(SetInBackups(5), SetDebug(true)) 153 | client.inbackups() 154 | if err != nil { 155 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 156 | } 157 | if err := client.Close(); err != nil { 158 | t.Fatalf("client.Close() Error: %q\n", err) 159 | } 160 | } 161 | 162 | func TestOptionOutBackups(t *testing.T) { 163 | client, err := NewClientFromOptions(SetOutBackups(5), SetDebug(true)) 164 | client.outbackups() 165 | if err != nil { 166 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 167 | } 168 | if err := client.Close(); err != nil { 169 | t.Fatalf("client.Close() Error: %q\n", err) 170 | } 171 | } 172 | 173 | func TestOptionEncryptLease(t *testing.T) { 174 | client, err := NewClientFromOptions(SetEncrypt(true), SetDebug(true)) 175 | if err != nil { 176 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 177 | } 178 | if err := client.Close(); err != nil { 179 | t.Fatalf("client.Close() Error: %q\n", err) 180 | } 181 | } 182 | 183 | func TestOptionUnpublishedLease(t *testing.T) { 184 | client, err := NewClientFromOptions(SetUnpublished(true), SetDebug(true)) 185 | if err != nil { 186 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 187 | } 188 | if err := client.Close(); err != nil { 189 | t.Fatalf("client.Close() Error: %q\n", err) 190 | } 191 | } 192 | 193 | func TestOptionReduceIdle(t *testing.T) { 194 | client, err := NewClientFromOptions(SetReduceIdle(true), SetDebug(true)) 195 | if err != nil { 196 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 197 | } 198 | if err := client.Close(); err != nil { 199 | t.Fatalf("client.Close() Error: %q\n", err) 200 | } 201 | } 202 | 203 | func TestOptionReduceIdleTime(t *testing.T) { 204 | client, err := NewClientFromOptions(SetReduceIdleTime(300001), SetDebug(true)) 205 | if err != nil { 206 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 207 | } 208 | if err := client.Close(); err != nil { 209 | t.Fatalf("client.Close() Error: %q\n", err) 210 | } 211 | } 212 | 213 | func TestOptionReduceIdleCount(t *testing.T) { 214 | client, err := NewClientFromOptions(SetReduceIdleQuantity(4), SetDebug(true)) 215 | if err != nil { 216 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 217 | } 218 | if err := client.Close(); err != nil { 219 | t.Fatalf("client.Close() Error: %q\n", err) 220 | } 221 | } 222 | 223 | func TestOptionCloseIdle(t *testing.T) { 224 | client, err := NewClientFromOptions(SetCloseIdle(true), SetDebug(true)) 225 | if err != nil { 226 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 227 | } 228 | if err := client.Close(); err != nil { 229 | t.Fatalf("client.Close() Error: %q\n", err) 230 | } 231 | } 232 | 233 | func TestOptionCloseIdleTime(t *testing.T) { 234 | client, err := NewClientFromOptions(SetCloseIdleTime(300001), SetDebug(true)) 235 | if err != nil { 236 | t.Fatalf("NewDefaultClient() Error: %q\n", err) 237 | } 238 | if err := client.Close(); err != nil { 239 | t.Fatalf("client.Close() Error: %q\n", err) 240 | } 241 | } 242 | -------------------------------------------------------------------------------- /replyParser.go: -------------------------------------------------------------------------------- 1 | package goSam 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | ) 7 | 8 | // The Possible Results send by SAM 9 | const ( 10 | ResultOk = "OK" //Operation completed successfully 11 | ResultCantReachPeer = "CANT_REACH_PEER" //The peer exists, but cannot be reached 12 | ResultDuplicatedID = "DUPLICATED_ID" //If the nickname is already associated with a session : 13 | ResultDuplicatedDest = "DUPLICATED_DEST" //The specified Destination is already in use 14 | ResultI2PError = "I2P_ERROR" //A generic I2P error (e.g. I2CP disconnection, etc.) 15 | ResultInvalidKey = "INVALID_KEY" //The specified key is not valid (bad format, etc.) 16 | ResultKeyNotFound = "KEY_NOT_FOUND" //The naming system can't resolve the given name 17 | ResultPeerNotFound = "PEER_NOT_FOUND" //The peer cannot be found on the network 18 | ResultTimeout = "TIMEOUT" // Timeout while waiting for an event (e.g. peer answer) 19 | ) 20 | 21 | // A ReplyError is a custom error type, containing the Result and full Reply 22 | type ReplyError struct { 23 | Result string 24 | Reply *Reply 25 | } 26 | 27 | func (r ReplyError) Error() string { 28 | return fmt.Sprintf("ReplyError: Result:%s - Reply:%+v", r.Result, r.Reply) 29 | } 30 | 31 | // Reply is the parsed result of a SAM command, containing a map of all the key-value pairs 32 | type Reply struct { 33 | Topic string 34 | Type string 35 | 36 | Pairs map[string]string 37 | } 38 | 39 | func parseReply(line string) (*Reply, error) { 40 | line = strings.TrimSpace(line) 41 | parts := strings.Split(line, " ") 42 | if len(parts) < 3 { 43 | return nil, fmt.Errorf("Malformed Reply.\n%s\n", line) 44 | } 45 | 46 | r := &Reply{ 47 | Topic: parts[0], 48 | Type: parts[1], 49 | Pairs: make(map[string]string, len(parts)-2), 50 | } 51 | 52 | for _, v := range parts[2:] { 53 | kvPair := strings.SplitN(v, "=", 2) 54 | if kvPair != nil { 55 | if len(kvPair) != 2 { 56 | return nil, fmt.Errorf("Malformed key-value-pair.\n%s\n", kvPair) 57 | } 58 | } 59 | 60 | r.Pairs[kvPair[0]] = kvPair[1] 61 | } 62 | 63 | return r, nil 64 | } 65 | -------------------------------------------------------------------------------- /replyParser_test.go: -------------------------------------------------------------------------------- 1 | package goSam 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | var validCases = []struct { 8 | Input string 9 | Expected Reply 10 | }{ 11 | // hello handshake reply 12 | { 13 | "HELLO REPLY RESULT=OK VERSION=3.0\n", 14 | Reply{ 15 | Topic: "HELLO", 16 | Type: "REPLY", 17 | Pairs: map[string]string{ 18 | "RESULT": "OK", 19 | "VERSION": "3.0", 20 | }, 21 | }, 22 | }, 23 | // result of a naming lookup 24 | { 25 | "NAMING REPLY RESULT=OK NAME=zzz.i2p VALUE=SomeValueForTesting\n", 26 | Reply{ 27 | Topic: "NAMING", 28 | Type: "REPLY", 29 | Pairs: map[string]string{ 30 | "RESULT": "OK", 31 | "NAME": "zzz.i2p", 32 | "VALUE": "SomeValueForTesting", 33 | }, 34 | }, 35 | }, 36 | // result of a b32.i2p naming lookup 37 | { 38 | "NAMING REPLY RESULT=OK NAME=gkso46tc47hdua2kva5zahj3unmyh6ia7bv5oc2ybn2hmeowpz7a.b32.i2p VALUE=mlHQraXLpcE7A4MVeVniRHM~2yoaW1fOYVKj3ZiNTe4UPIAlIReYUMHSnZnloFWX7bh2OoEg08JBGoSQPtGkCZjqSBmfeDdMqtwbZ~~sok-jNo4e5rWnfCOHYYPVcuE8jB~7M5ioJzk8QZRqh3AjCQsKBUZgTzUfGlP12j3GtAf5C9iAGxTTB1sGE96752EKP0dzyGOs4NAujwkgm6NzVoqlkXD~fognUrQOeG~~OqChsAeqIRqj40FsJmsJREmZ4GhjFAqzLUQ4hMpKSbqMI~wtfjeNs-WKtM7pCD09uwSmYwW84wu-WxLGZiIt2GKmbPv~JrqYFNv9EM0SzUonAF8pw9GAhUn8-26kkgCXTs05Kut7NuQHghu3jHfS-frlPmAt-Uu5T4ZcLiHiFrnG2lYTtjxBFXh7W72IBncHSixhVhd4lYM7REKFj7G~5ttW9EBeL1unbNYWiQOEQjtGlmwxYt~~2EV16w339aQQ~S~69-tS6vFA1n2DgkMdg06pBQAEAAEAAA==\n", 39 | Reply{ 40 | Topic: "NAMING", 41 | Type: "REPLY", 42 | Pairs: map[string]string{ 43 | "RESULT": "OK", 44 | "NAME": "gkso46tc47hdua2kva5zahj3unmyh6ia7bv5oc2ybn2hmeowpz7a.b32.i2p", 45 | "VALUE": "mlHQraXLpcE7A4MVeVniRHM~2yoaW1fOYVKj3ZiNTe4UPIAlIReYUMHSnZnloFWX7bh2OoEg08JBGoSQPtGkCZjqSBmfeDdMqtwbZ~~sok-jNo4e5rWnfCOHYYPVcuE8jB~7M5ioJzk8QZRqh3AjCQsKBUZgTzUfGlP12j3GtAf5C9iAGxTTB1sGE96752EKP0dzyGOs4NAujwkgm6NzVoqlkXD~fognUrQOeG~~OqChsAeqIRqj40FsJmsJREmZ4GhjFAqzLUQ4hMpKSbqMI~wtfjeNs-WKtM7pCD09uwSmYwW84wu-WxLGZiIt2GKmbPv~JrqYFNv9EM0SzUonAF8pw9GAhUn8-26kkgCXTs05Kut7NuQHghu3jHfS-frlPmAt-Uu5T4ZcLiHiFrnG2lYTtjxBFXh7W72IBncHSixhVhd4lYM7REKFj7G~5ttW9EBeL1unbNYWiQOEQjtGlmwxYt~~2EV16w339aQQ~S~69-tS6vFA1n2DgkMdg06pBQAEAAEAAA==", 46 | }, 47 | }, 48 | }, 49 | // session status reply 50 | { 51 | "SESSION STATUS RESULT=I2P_ERROR MESSAGE=TheMessageFromI2p\n", 52 | Reply{ 53 | Topic: "SESSION", 54 | Type: "STATUS", 55 | Pairs: map[string]string{ 56 | "RESULT": "I2P_ERROR", 57 | "MESSAGE": "TheMessageFromI2p", 58 | }, 59 | }, 60 | }, 61 | { 62 | "STREAM STATUS RESULT=CANT_REACH_PEER\n", 63 | Reply{ 64 | Topic: "STREAM", 65 | Type: "STATUS", 66 | Pairs: map[string]string{ 67 | "RESULT": "CANT_REACH_PEER", 68 | }, 69 | }, 70 | }, 71 | } 72 | 73 | func TestParseReplyValidCases(t *testing.T) { 74 | for _, tcase := range validCases { 75 | parsed, err := parseReply(tcase.Input) 76 | if err != nil { 77 | t.Fatalf("parseReply should not throw an error!\n%s", err) 78 | } 79 | 80 | if parsed.Topic != tcase.Expected.Topic { 81 | t.Fatalf("Wrong Topic. Got %s expected %s", parsed.Topic, tcase.Expected.Topic) 82 | } 83 | 84 | if len(parsed.Pairs) != len(tcase.Expected.Pairs) { 85 | t.Fatalf("Wrong ammount of Pairs. Got %d expected 3", len(parsed.Pairs)) 86 | } 87 | 88 | for expK, expV := range tcase.Expected.Pairs { 89 | if expV != parsed.Pairs[expK] { 90 | t.Fatalf("Wrong %s.\nGot '%s'\nExpected '%s'", expK, parsed.Pairs[expK], expV) 91 | } 92 | } 93 | } 94 | } 95 | 96 | func TestParseInvalidReply(t *testing.T) { 97 | str := "asd asd=" 98 | 99 | r, err := parseReply(str) 100 | if err == nil { 101 | t.Fatalf("Should throw an error.r:%v\n", r) 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /sessions.go: -------------------------------------------------------------------------------- 1 | package goSam 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | "math/rand" 7 | "time" 8 | ) 9 | 10 | func init() { 11 | rand.Seed(time.Now().UnixNano()) 12 | } 13 | 14 | // CreateStreamSession creates a new STREAM Session. 15 | // Returns the Id for the new Client. 16 | func (c *Client) CreateStreamSession(dest string) (int32, string, error) { 17 | if dest == "" { 18 | dest = "TRANSIENT" 19 | } 20 | 21 | id := rand.Int31n(math.MaxInt32) 22 | r, err := c.sendCmd("SESSION CREATE STYLE=STREAM ID=%d DESTINATION=%s %s\n", id, dest, c.allOptions()) 23 | if err != nil { 24 | return -1, "", err 25 | } 26 | 27 | // TODO: move check into sendCmd() 28 | if r.Topic != "SESSION" || r.Type != "STATUS" { 29 | return -1, "", fmt.Errorf("Unknown Reply: %+v\n", r) 30 | } 31 | 32 | result := r.Pairs["RESULT"] 33 | if result != "OK" { 34 | return -1, "", ReplyError{ResultKeyNotFound, r} 35 | } 36 | 37 | return id, r.Pairs["DESTINATION"], nil 38 | } 39 | -------------------------------------------------------------------------------- /stream.go: -------------------------------------------------------------------------------- 1 | package goSam 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | // StreamConnect asks SAM for a TCP-Like connection to dest, has to be called on a new Client 8 | func (c *Client) StreamConnect(id int32, dest string) error { 9 | r, err := c.sendCmd("STREAM CONNECT ID=%d DESTINATION=%s\n", id, dest) 10 | if err != nil { 11 | return err 12 | } 13 | 14 | // TODO: move check into sendCmd() 15 | if r.Topic != "STREAM" || r.Type != "STATUS" { 16 | return fmt.Errorf("Unknown Reply: %+v\n", r) 17 | } 18 | 19 | result := r.Pairs["RESULT"] 20 | if result != "OK" { 21 | return ReplyError{result, r} 22 | } 23 | 24 | return nil 25 | } 26 | 27 | // StreamAccept asks SAM to accept a TCP-Like connection 28 | func (c *Client) StreamAccept(id int32) (*Reply, error) { 29 | r, err := c.sendCmd("STREAM ACCEPT ID=%d SILENT=false\n", id) 30 | if err != nil { 31 | return nil, err 32 | } 33 | 34 | // TODO: move check into sendCmd() 35 | if r.Topic != "STREAM" || r.Type != "STATUS" { 36 | return nil, fmt.Errorf("Unknown Reply: %+v\n", r) 37 | } 38 | 39 | result := r.Pairs["RESULT"] 40 | if result != "OK" { 41 | return nil, ReplyError{result, r} 42 | } 43 | 44 | return r, nil 45 | } 46 | --------------------------------------------------------------------------------