Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | package common |
| 21 | |
| 22 | import ( |
Jens Geyer | c6b991f | 2015-08-07 23:41:09 +0200 | [diff] [blame] | 23 | "compress/zlib" |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 24 | "crypto/tls" |
| 25 | "flag" |
| 26 | "fmt" |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 27 | "net/http" |
Yuxuan 'fishy' Wang | b71f11e | 2021-03-22 15:01:00 -0700 | [diff] [blame] | 28 | |
| 29 | "github.com/apache/thrift/lib/go/thrift" |
| 30 | "github.com/apache/thrift/test/go/src/gen/thrifttest" |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | var debugClientProtocol bool |
| 34 | |
| 35 | func init() { |
| 36 | flag.BoolVar(&debugClientProtocol, "debug_client_protocol", false, "turn client protocol trace on") |
| 37 | } |
| 38 | |
| 39 | func StartClient( |
| 40 | host string, |
| 41 | port int64, |
| 42 | domain_socket string, |
| 43 | transport string, |
| 44 | protocol string, |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 45 | ssl bool, |
| 46 | ) (client *thrifttest.ThriftTestClient, trans thrift.TTransport, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 47 | hostPort := fmt.Sprintf("%s:%d", host, port) |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 48 | cfg := &thrift.TConfiguration{ |
| 49 | TLSConfig: &tls.Config{ |
| 50 | InsecureSkipVerify: true, |
| 51 | }, |
| 52 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 53 | |
| 54 | var protocolFactory thrift.TProtocolFactory |
| 55 | switch protocol { |
| 56 | case "compact": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 57 | protocolFactory = thrift.NewTCompactProtocolFactoryConf(cfg) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 58 | case "simplejson": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 59 | protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(cfg) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 60 | case "json": |
| 61 | protocolFactory = thrift.NewTJSONProtocolFactory() |
| 62 | case "binary": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 63 | protocolFactory = thrift.NewTBinaryProtocolFactoryConf(cfg) |
Yuxuan 'fishy' Wang | 4d46c11 | 2019-06-07 20:47:18 +0800 | [diff] [blame] | 64 | case "header": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 65 | protocolFactory = thrift.NewTHeaderProtocolFactoryConf(cfg) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 66 | default: |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 67 | return nil, nil, fmt.Errorf("invalid protocol specified %s", protocol) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 68 | } |
| 69 | if debugClientProtocol { |
Yuxuan 'fishy' Wang | fb539ae | 2021-08-09 14:27:48 -0700 | [diff] [blame] | 70 | protocolFactory = thrift.NewTDebugProtocolFactoryWithLogger(protocolFactory, "client:", thrift.StdLogger(nil)) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 71 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 72 | if ssl { |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 73 | trans = thrift.NewTSSLSocketConf(hostPort, cfg) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 74 | } else { |
| 75 | if domain_socket != "" { |
Yuxuan 'fishy' Wang | fb539ae | 2021-08-09 14:27:48 -0700 | [diff] [blame] | 76 | trans = thrift.NewTSocketConf(domain_socket, nil) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 77 | } else { |
Yuxuan 'fishy' Wang | fb539ae | 2021-08-09 14:27:48 -0700 | [diff] [blame] | 78 | trans = thrift.NewTSocketConf(hostPort, nil) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | if err != nil { |
D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 82 | return nil, nil, err |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 83 | } |
| 84 | switch transport { |
| 85 | case "http": |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 86 | if ssl { |
| 87 | tr := &http.Transport{ |
| 88 | TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 89 | } |
| 90 | client := &http.Client{Transport: tr} |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 91 | trans, err = thrift.NewTHttpClientWithOptions(fmt.Sprintf("https://%s/", hostPort), thrift.THttpClientOptions{Client: client}) |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 92 | fmt.Println(hostPort) |
| 93 | } else { |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 94 | trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/", hostPort)) |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 95 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 96 | case "framed": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 97 | trans = thrift.NewTFramedTransportConf(trans, cfg) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 98 | case "buffered": |
| 99 | trans = thrift.NewTBufferedTransport(trans, 8192) |
Jens Geyer | c6b991f | 2015-08-07 23:41:09 +0200 | [diff] [blame] | 100 | case "zlib": |
| 101 | trans, err = thrift.NewTZlibTransport(trans, zlib.BestCompression) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 102 | case "": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 103 | // Do nothing |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 104 | default: |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 105 | return nil, nil, fmt.Errorf("invalid transport specified %s", transport) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 106 | } |
D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 107 | if err != nil { |
| 108 | return nil, nil, err |
| 109 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 110 | if err = trans.Open(); err != nil { |
D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 111 | return nil, nil, err |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 112 | } |
D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 113 | iprot := protocolFactory.GetProtocol(trans) |
| 114 | oprot := protocolFactory.GetProtocol(trans) |
| 115 | client = thrifttest.NewThriftTestClient(thrift.NewTStandardClient(iprot, oprot)) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 116 | return |
| 117 | } |