├── .travis.yml ├── LICENSE ├── README.md ├── client.go ├── client_test.go ├── go.mod └── priority.go /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | 3 | go: 4 | - "1.7.4" 5 | - "1.7.6" 6 | - "1.8.7" 7 | - "1.9.5" 8 | - "1.10.1" 9 | - master 10 | 11 | go_import_path: github.com/Neo23x0/simplesyslog 12 | notifications: 13 | email: false 14 | slack: nextron-systems:51vB5h6MxkSbX5iMpscrvOza 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # simplesyslog 2 | 3 | [![Build Status](https://travis-ci.org/NextronSystems/simplesyslog.svg?branch=master)](https://travis-ci.org/NextronSystems/simplesyslog) 4 | 5 | Simple SYSLOG client in Go 6 | 7 | ## Installation 8 | 9 | `go get github.com/NextronSystems/simplesyslog` 10 | 11 | ## Example Usage 12 | 13 | ```go 14 | 15 | import ( 16 | syslog "github.com/NextronSystems/simplesyslog" 17 | ) 18 | 19 | const SyslogServer = ":" 20 | 21 | func main() { 22 | client, err := syslog.NewClient(syslog.ConnectionUDP, SyslogServer) 23 | if err != nil { 24 | ... 25 | } 26 | defer client.Close() 27 | if err := client.Send("foo bar baz", syslog.LOG_LOCAL0|syslog.LOG_NOTICE); err != nil { 28 | ... 29 | } 30 | } 31 | ``` 32 | -------------------------------------------------------------------------------- /client.go: -------------------------------------------------------------------------------- 1 | // Simple Syslog Server that 2 | // supports UDP, TCP and TLS. 3 | // 4 | // Marcel Gebhardt 5 | // April 2018 6 | 7 | package simplesyslog 8 | 9 | import ( 10 | "crypto/tls" 11 | "errors" 12 | "fmt" 13 | "net" 14 | "os" 15 | "strings" 16 | "time" 17 | ) 18 | 19 | // ConnectionType defines wheather to connect via UDP or TCP (or TLS) 20 | type ConnectionType string 21 | 22 | const ( 23 | // ConnectionUDP connects via UDP 24 | ConnectionUDP ConnectionType = "udp" 25 | // ConnectionTCP connects via TCP 26 | ConnectionTCP ConnectionType = "tcp" 27 | // ConnectionTLS connects via TLS 28 | ConnectionTLS ConnectionType = "tls" 29 | ) 30 | 31 | const ( 32 | // DefaultHostname will be used if hostname could not be determined 33 | DefaultHostname string = "unknown" 34 | // DefaultIP will be used if ip could not be determined 35 | DefaultIP string = "" 36 | ) 37 | 38 | const ( 39 | Rfc3164 = 1024 40 | Rfc5424 = 2048 41 | Unlimited = 0 42 | ) 43 | 44 | // Client holds a connection to a specified address 45 | type Client struct { 46 | Hostname string // Hostname of the system 47 | IP string // IP of the system 48 | Rfc3339 bool // use rfc3339 instead of stamp for time format 49 | MaxLength int // max syslog length 50 | NoPrio bool // do not add Prefix 51 | HostnameOnly bool // Only use hostname in syslog header instead of hostname ip combination 52 | 53 | conn net.Conn // connection to the syslog server 54 | bytesSent int64 55 | maxBytes int64 56 | } 57 | 58 | // NewClient initializes a new server connection. 59 | // Examples: 60 | // - NewClient(ConnectionUDP, "172.0.0.1:514") 61 | // - NewClient(ConnectionTCP, ":514") 62 | // - NewClient(ConnectionTLS, "172.0.0.1:514") 63 | func NewClient(connectionType ConnectionType, address string, tlsconfig *tls.Config) (*Client, error) { 64 | // Validate data 65 | if connectionType != ConnectionUDP && connectionType != ConnectionTCP && connectionType != ConnectionTLS { 66 | return nil, fmt.Errorf("unknown connection type '%s'", connectionType) 67 | } 68 | var ( 69 | conn net.Conn 70 | err error 71 | ) 72 | // connect via udp / tcp / tls 73 | if connectionType == ConnectionTLS { 74 | conn, err = tls.Dial(string(ConnectionTCP), address, tlsconfig) 75 | } else { 76 | conn, err = net.Dial(string(connectionType), address) 77 | } 78 | if err != nil { 79 | return nil, err 80 | } 81 | // get hostname and ip of system 82 | hostname, err := os.Hostname() 83 | if err != nil { 84 | hostname = DefaultHostname 85 | } 86 | ip, _, err := net.SplitHostPort(conn.LocalAddr().String()) 87 | if err != nil { 88 | ip = DefaultIP 89 | } 90 | // return the server 91 | return &Client{ 92 | Hostname: hostname, 93 | IP: ip, 94 | conn: conn, 95 | }, nil 96 | } 97 | 98 | // TooManyBytesSentErr will be returned, if a message could not be sent 99 | // because of a hard limit of bytes to be send. 100 | var TooManyBytesSentErr = errors.New("too many bytes sent") 101 | 102 | // Send sends a syslog message with a specified priority. 103 | // Examples: 104 | // - Send("foo", LOG_LOCAL0|LOG_NOTICE) 105 | // - Send("bar", LOG_DAEMON|LOG_DEBUG) 106 | func (client *Client) Send(message string, priority Priority) error { 107 | if client.maxBytes != 0 && client.bytesSent > client.maxBytes { 108 | return TooManyBytesSentErr 109 | } 110 | var timestamp string 111 | if client.Rfc3339 { 112 | timestamp = time.Now().UTC().Format(time.RFC3339) 113 | } else { 114 | timestamp = time.Now().UTC().Format(time.Stamp) 115 | } 116 | var hostnameCombi = client.Hostname 117 | if !client.HostnameOnly { 118 | hostnameCombi = fmt.Sprintf("%s/%s", client.Hostname, client.IP) 119 | } 120 | var header string 121 | if client.NoPrio { 122 | header = fmt.Sprintf("%s %s", timestamp, hostnameCombi) 123 | } else { 124 | header = fmt.Sprintf("<%d>%s %s", int(priority), timestamp, hostnameCombi) 125 | } 126 | // RFC length reduction 127 | length := len(message) 128 | if client.MaxLength > 3 && length > client.MaxLength { 129 | message = fmt.Sprintf("%s...", message[:client.MaxLength-3]) 130 | } 131 | message = strings.TrimSuffix(message, "\n") 132 | // Send message 133 | // While a terminating line break is not required in the RFC, it is "informal standard", 134 | // especially for data streams like TCP where we don't have the "one message per datagram" concept. 135 | n, err := fmt.Fprintf(client.conn, "%s %s\n", header, message) 136 | client.bytesSent += int64(n) 137 | return err 138 | } 139 | 140 | // SendRaw sends a syslog message without adding syslog header. 141 | // Examples: 142 | // - SendRaw("foo") 143 | // - SendRaw("bar") 144 | func (client *Client) SendRaw(message string) error { 145 | if client.maxBytes != 0 && client.bytesSent > client.maxBytes { 146 | return TooManyBytesSentErr 147 | } 148 | // RFC length reduction 149 | length := len(message) 150 | if client.MaxLength > 3 && length > client.MaxLength { 151 | message = fmt.Sprintf("%s...", message[:client.MaxLength-3]) 152 | } 153 | if !strings.HasSuffix(message, "\n") { 154 | message += "\n" 155 | } 156 | // Send message 157 | n, err := fmt.Fprint(client.conn, message) 158 | client.bytesSent += int64(n) 159 | return err 160 | } 161 | 162 | // Close closes the server connection gracefully. 163 | func (client *Client) Close() error { 164 | return client.conn.Close() 165 | } 166 | 167 | // SetMaxBytes sets the maximum bytes that will be sent to rsyslog (approximately) 168 | func (client *Client) SetMaxBytes(i int64) { 169 | client.maxBytes = i 170 | } 171 | -------------------------------------------------------------------------------- /client_test.go: -------------------------------------------------------------------------------- 1 | package simplesyslog 2 | 3 | import ( 4 | "crypto/tls" 5 | "crypto/x509" 6 | "encoding/pem" 7 | "io/ioutil" 8 | "net" 9 | "regexp" 10 | "strconv" 11 | "testing" 12 | "time" 13 | ) 14 | 15 | const host = "127.0.0.1:15140" 16 | 17 | const tlsCRT = `-----BEGIN CERTIFICATE----- 18 | MIICJzCCAZACCQCPGY+4vjNV0TANBgkqhkiG9w0BAQUFADBYMQswCQYDVQQGEwJE 19 | RTEPMA0GA1UECAwGSGVzc2VuMRIwEAYDVQQHDAlGcmFua2Z1cnQxEjAQBgNVBAoM 20 | CUNvZGVoYXJkdDEQMA4GA1UEAwwHVGVzdGluZzAeFw0xODA0MjAwNzUwMzZaFw0x 21 | OTA0MjEwNzUwMzZaMFgxCzAJBgNVBAYTAkRFMQ8wDQYDVQQIDAZIZXNzZW4xEjAQ 22 | BgNVBAcMCUZyYW5rZnVydDESMBAGA1UECgwJQ29kZWhhcmR0MRAwDgYDVQQDDAdU 23 | ZXN0aW5nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcHWCPUdd4VfxorTjk 24 | 5g/97HiAdcVn2kbDEVv2aI9HdSbcC9DC209DoaX4/7+3cEf3TwE5RKu9acf8tDue 25 | W8tAWvKH4wW7hIHiipfhFisuQeLe5NgXGqY+bs+B5+A0C/rKTrGkHu8hpXjFPY2y 26 | rwyYMBPmIm44X53tzsNYzuQakQIDAQABMA0GCSqGSIb3DQEBBQUAA4GBAL5k/7HU 27 | g2+6QcrV2K+2616D0ssgFrKqyG1dTy9w2+jZPQWcVNTRXGDkfdK/JlXzfEQwI/bZ 28 | 89GmxswWxoxoMi7ZMPAG2h66vMUwCFTjUGEihgX/qksnzglMTQHlgLGENfQfawy1 29 | G1MpWJaW2ClQGr70dTTFeFJLSOANdAEqkTOC 30 | -----END CERTIFICATE-----` 31 | 32 | const tlsKEY = `-----BEGIN RSA PRIVATE KEY----- 33 | MIICXgIBAAKBgQDcHWCPUdd4VfxorTjk5g/97HiAdcVn2kbDEVv2aI9HdSbcC9DC 34 | 209DoaX4/7+3cEf3TwE5RKu9acf8tDueW8tAWvKH4wW7hIHiipfhFisuQeLe5NgX 35 | GqY+bs+B5+A0C/rKTrGkHu8hpXjFPY2yrwyYMBPmIm44X53tzsNYzuQakQIDAQAB 36 | AoGBAI8JmCIKcRcF6YysZHh6+JFuBbCU179xHOLOeRBbSiCJhMMh+ntlwNCWTyDM 37 | MW2nTVzsvkLU2TWxdABHryZtSFpJIq69euzRtEN3uFy5qJWGvlE6Tn+ps7XIbPsX 38 | rppeclgV7a2nznrh+v1hfY/hgePyhuDsH0Hh5HB+L/gdb5DxAkEA81IPoy/UZWKg 39 | wIGeGy0mWyb4rmIxJTxOQPHd+2iJaD173eY0PskWuMLHFjyVn2gqHdy88ZDe0Xh9 40 | 35SjOw5J7wJBAOeVvye1hnvOdgOHpuurDwvoTy/A+hUhzuzPKyUhNwENHk1d5L8D 41 | w2n+onITuFhRUvJW+ZCn+8BcY2Q0FNUqY38CQQCs7WhhsQ+BkqvuxPAKHneBFtxs 42 | iyqkbQysiXkbQXtOk0viM8ZzzNSSMRPvENXBufUczhGWmUBSnRDQgsHTqd8PAkAv 43 | Wdbz75HHzrcikaH3nco9zQoj4XlAyODeWp2fweLVPDFt8DzNMZ/LFF1ypcWTiU1E 44 | b7Qnd7Fp63oHCv8XdstRAkEA5Lf2nA2rEi5WxvSIea5KUzQp6Ut1aCLjHpdU5Pk7 45 | 4IqCgyaC9pPvCkL6rEOthAfh9nnPJp41zMk7jHz5zmRe6g== 46 | -----END RSA PRIVATE KEY-----` 47 | 48 | var tlsConfig *tls.Config 49 | 50 | var messageRegex = regexp.MustCompile(`<133>[A-Z][a-z]{2} (([0-9]{2})|( [0-9])) [0-9]{2}:[0-9]{2}:[0-9]{2} testing\/127\.0\.0\.1 foo bar baz`) 51 | 52 | func TestNewClient(t *testing.T) { 53 | testNewClientTCP(t, false) 54 | testNewClientTCP(t, true) 55 | testNewClientUDP(t) 56 | } 57 | 58 | func testNewClientUDP(t *testing.T) { 59 | t.Logf("testing udp") 60 | serverAddr, err := net.ResolveUDPAddr("udp", host) 61 | if err != nil { 62 | t.Fatalf("could not resolve udp addr: %s", err) 63 | } 64 | conn, err := net.ListenUDP("udp", serverAddr) 65 | if err != nil { 66 | t.Fatalf("could not listen udp: %s", err) 67 | } 68 | if err := conn.SetReadDeadline(time.Now().Add(time.Second * 5)); err != nil { 69 | t.Fatalf("could not set read deadline: %s", err) 70 | } 71 | defer conn.Close() 72 | go func() { 73 | /* 74 | * Send the message 'foo bar baz' to the syslog server 75 | */ 76 | client, err := NewClient(ConnectionUDP, host, nil) 77 | if err != nil { 78 | t.Fatalf("could not initialize server: %s", err) 79 | } 80 | client.Hostname = "testing" // overwrite hostname for testing 81 | defer client.Close() 82 | if err := client.Send("foo bar baz", LOG_LOCAL0|LOG_NOTICE); err != nil { 83 | t.Fatalf("could not send message: %s", err) 84 | } 85 | }() 86 | buf := make([]byte, 1024) 87 | n, _, err := conn.ReadFrom(buf) 88 | if err != nil { 89 | t.Fatalf("could not read udp: %s", err) 90 | } 91 | b := buf[:n] 92 | if !messageRegex.MatchString(string(b)) { 93 | t.Fatalf("wrong message: %s", string(b)) 94 | } else { 95 | t.Logf("correct message: '%s'", string(b)) 96 | } 97 | } 98 | 99 | func testNewClientTCP(t *testing.T, useTLS bool) { 100 | t.Logf("testing tcp with tls '%s'", strconv.FormatBool(useTLS)) 101 | var ( 102 | listener net.Listener 103 | err error 104 | ) 105 | if useTLS { 106 | listener, err = tls.Listen("tcp", host, tlsConfig) 107 | } else { 108 | listener, err = net.Listen("tcp", host) 109 | } 110 | if err != nil { 111 | t.Fatalf("could not listen: %s", err) 112 | } 113 | defer listener.Close() 114 | go func() { 115 | /* 116 | * Send the message 'foo bar baz' to the syslog server 117 | */ 118 | connectionType := ConnectionTCP 119 | if useTLS { 120 | connectionType = ConnectionTLS 121 | } 122 | client, err := NewClient(connectionType, host, &tls.Config{InsecureSkipVerify: true}) 123 | if err != nil { 124 | t.Fatalf("could not initialize server: %s", err) 125 | } 126 | client.Hostname = "testing" // overwrite hostname for testing 127 | defer client.Close() 128 | if err := client.Send("foo bar baz", LOG_LOCAL0|LOG_NOTICE); err != nil { 129 | t.Fatalf("could not send message: %s", err) 130 | } 131 | }() 132 | for { 133 | conn, err := listener.Accept() 134 | if err != nil { 135 | t.Fatalf("could not accept connection: %s", err) 136 | } 137 | defer conn.Close() 138 | b, err := ioutil.ReadAll(conn) 139 | if err != nil { 140 | t.Fatalf("could not read all: %s", err) 141 | } 142 | if !messageRegex.MatchString(string(b)) { 143 | t.Fatalf("wrong message: %s", string(b)) 144 | } else { 145 | t.Logf("correct message: '%s'", string(b)) 146 | } 147 | return 148 | } 149 | } 150 | 151 | func init() { 152 | pemCert, _ := pem.Decode([]byte(tlsCRT)) 153 | if pemCert == nil { 154 | panic("no test tls certificate") 155 | } 156 | pemKey, _ := pem.Decode([]byte(tlsKEY)) 157 | if pemKey == nil { 158 | panic("no test tls key") 159 | } 160 | privateKey, err := x509.ParsePKCS1PrivateKey(pemKey.Bytes) 161 | if err != nil { 162 | panic("invalid test tls key") 163 | } 164 | cert := &tls.Certificate{ 165 | Certificate: [][]byte{pemCert.Bytes}, 166 | PrivateKey: privateKey, 167 | } 168 | tlsConfig = &tls.Config{ 169 | Certificates: []tls.Certificate{*cert}, 170 | MinVersion: tls.VersionTLS11, 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/NextronSystems/simplesyslog 2 | 3 | go 1.20 4 | -------------------------------------------------------------------------------- /priority.go: -------------------------------------------------------------------------------- 1 | package simplesyslog 2 | 3 | // Priority is the combination of severity and facility. 4 | // (see go's syslog package for more infos) 5 | type Priority int 6 | 7 | // Severities 8 | const ( 9 | LOG_EMERG Priority = iota 10 | LOG_ALERT 11 | LOG_CRIT 12 | LOG_ERR 13 | LOG_WARNING 14 | LOG_NOTICE 15 | LOG_INFO 16 | LOG_DEBUG 17 | ) 18 | 19 | // Facilities 20 | const ( 21 | LOG_KERN Priority = iota << 3 22 | LOG_USER 23 | LOG_MAIL 24 | LOG_DAEMON 25 | LOG_AUTH 26 | LOG_SYSLOG 27 | LOG_LPR 28 | LOG_NEWS 29 | LOG_UUCP 30 | LOG_CRON 31 | LOG_AUTHPRIV 32 | LOG_FTP 33 | _ // unused 34 | _ // unused 35 | _ // unused 36 | _ // unused 37 | LOG_LOCAL0 38 | LOG_LOCAL1 39 | LOG_LOCAL2 40 | LOG_LOCAL3 41 | LOG_LOCAL4 42 | LOG_LOCAL5 43 | LOG_LOCAL6 44 | LOG_LOCAL7 45 | ) 46 | --------------------------------------------------------------------------------