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( |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame] | 40 | addr string, |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 41 | transport string, |
| 42 | protocol string, |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 43 | ssl bool, |
| 44 | ) (client *thrifttest.ThriftTestClient, trans thrift.TTransport, err error) { |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 45 | cfg := &thrift.TConfiguration{ |
| 46 | TLSConfig: &tls.Config{ |
| 47 | InsecureSkipVerify: true, |
| 48 | }, |
| 49 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 50 | |
| 51 | var protocolFactory thrift.TProtocolFactory |
| 52 | switch protocol { |
| 53 | case "compact": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 54 | protocolFactory = thrift.NewTCompactProtocolFactoryConf(cfg) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 55 | case "simplejson": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 56 | protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(cfg) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 57 | case "json": |
| 58 | protocolFactory = thrift.NewTJSONProtocolFactory() |
| 59 | case "binary": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 60 | protocolFactory = thrift.NewTBinaryProtocolFactoryConf(cfg) |
Yuxuan 'fishy' Wang | 4d46c11 | 2019-06-07 20:47:18 +0800 | [diff] [blame] | 61 | case "header": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 62 | protocolFactory = thrift.NewTHeaderProtocolFactoryConf(cfg) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 63 | default: |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 64 | return nil, nil, fmt.Errorf("invalid protocol specified %s", protocol) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 65 | } |
| 66 | if debugClientProtocol { |
Yuxuan 'fishy' Wang | fb539ae | 2021-08-09 14:27:48 -0700 | [diff] [blame] | 67 | protocolFactory = thrift.NewTDebugProtocolFactoryWithLogger(protocolFactory, "client:", thrift.StdLogger(nil)) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 68 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 69 | if ssl { |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame] | 70 | trans = thrift.NewTSSLSocketConf(addr, cfg) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 71 | } else { |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame] | 72 | trans = thrift.NewTSocketConf(addr, nil) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 73 | } |
| 74 | if err != nil { |
D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 75 | return nil, nil, err |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 76 | } |
| 77 | switch transport { |
| 78 | case "http": |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 79 | if ssl { |
| 80 | tr := &http.Transport{ |
| 81 | TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 82 | } |
| 83 | client := &http.Client{Transport: tr} |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame] | 84 | trans, err = thrift.NewTHttpClientWithOptions(fmt.Sprintf("https://%s/", addr), thrift.THttpClientOptions{Client: client}) |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 85 | } else { |
Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame] | 86 | trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/", addr)) |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 87 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 88 | case "framed": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 89 | trans = thrift.NewTFramedTransportConf(trans, cfg) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 90 | case "buffered": |
| 91 | trans = thrift.NewTBufferedTransport(trans, 8192) |
Jens Geyer | c6b991f | 2015-08-07 23:41:09 +0200 | [diff] [blame] | 92 | case "zlib": |
| 93 | trans, err = thrift.NewTZlibTransport(trans, zlib.BestCompression) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 94 | case "": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 95 | // Do nothing |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 96 | default: |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 97 | return nil, nil, fmt.Errorf("invalid transport specified %s", transport) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 98 | } |
D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 99 | if err != nil { |
| 100 | return nil, nil, err |
| 101 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 102 | if err = trans.Open(); err != nil { |
D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 103 | return nil, nil, err |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 104 | } |
D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 105 | iprot := protocolFactory.GetProtocol(trans) |
| 106 | oprot := protocolFactory.GetProtocol(trans) |
| 107 | client = thrifttest.NewThriftTestClient(thrift.NewTStandardClient(iprot, oprot)) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 108 | return |
| 109 | } |