Copyright date updates.
[stompngo_examples.git] / tlsexamps / tlsuc1 / tlsuc1.go
blob693a76766cbb59814e35b5479ba0916a571acbed
1 //
2 // Copyright © 2013-2018 Guy M. Allard
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 Connect and Disconnect from a STOMP broker with a TLS connection, use case 1.
20 TLS Use Case 1 - client does *not* authenticate broker.
22 Subcase 1.A - Message broker configuration does *not* require client authentication
24 - Expect connection success
26 Subcase 1.B - Message broker configuration *does* require client authentication
28 - Expect connection failure (broker must be sent a valid client certificate)
30 Example use might be:
32 go build
33 STOMP_PORT=61611 ./tlsuc1
36 package main
38 import (
39 "crypto/tls"
40 "log"
41 "os"
42 "time"
43 // sngecomm methods are used specifically for these example clients.
44 "github.com/gmallard/stompngo_examples/sngecomm"
47 var (
48 exampid = "tlsuc1: "
49 tc *tls.Config
51 ll = log.New(os.Stdout, "TLSU1 ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
53 tag = "tuc1main"
56 // Connect to a STOMP broker using TLS and disconnect.
57 func main() {
59 st := time.Now()
61 ll.Printf("%stag:%s connsess:%s starts\n",
62 exampid, tag, sngecomm.Lcs)
64 // TLS Configuration.
65 tc = new(tls.Config)
66 tc.InsecureSkipVerify = true // Do *not* check the server's certificate
68 // Usually one will use the default cipher suites that go provides.
69 // However, if a custom cipher squite list is needed/required this
70 // is how it is accomplished.
71 if sngecomm.UseCustomCiphers() { // Set custom cipher suite list
72 tc.CipherSuites = append(tc.CipherSuites, sngecomm.CustomCiphers()...)
75 // Standard example TLS connect sequence
76 n, conn, e := sngecomm.CommonTLSConnect(exampid, tag, ll, tc)
77 if e != nil {
78 ll.Fatalf("%stag:%s connsess:%s main_on_connect error:%v",
79 exampid, tag, sngecomm.Lcs,
80 e.Error()) // Handle this ......
83 nc := n.(*tls.Conn)
84 sngecomm.DumpTLSConfig(exampid, tc, nc)
86 // *NOTE* application specific functionaltiy starts here!
87 // For you to add.
88 // *NOTE* application specific functionaltiy ends here!
90 // Standard example disconnect sequence
91 e = sngecomm.CommonDisconnect(n, conn, exampid, tag, ll)
92 if e != nil {
93 ll.Fatalf("%s %s\n", exampid, e.Error()) // Handle this ......
96 ll.Printf("%stag:%s connsess:%s main_elapsed:%v\n",
97 exampid, tag, conn.Session(),
98 time.Now().Sub(st))