|
| 1 | +// Copyright 2019-2024 The Liqo Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package proxy |
| 16 | + |
| 17 | +import ( |
| 18 | + "bufio" |
| 19 | + "io" |
| 20 | + "net" |
| 21 | + "net/http" |
| 22 | + "time" |
| 23 | + |
| 24 | + "k8s.io/klog/v2" |
| 25 | +) |
| 26 | + |
| 27 | +func (p *Proxy) handleConnect(c net.Conn) { |
| 28 | + br := bufio.NewReader(c) |
| 29 | + req, err := http.ReadRequest(br) |
| 30 | + if err != nil { |
| 31 | + klog.Errorf("error reading request: %v", err) |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + if req.Method != http.MethodConnect { |
| 36 | + response := &http.Response{ |
| 37 | + StatusCode: http.StatusMethodNotAllowed, |
| 38 | + ProtoMajor: 1, |
| 39 | + ProtoMinor: 1, |
| 40 | + } |
| 41 | + if err := response.Write(c); err != nil { |
| 42 | + klog.Errorf("error writing response: %v", err) |
| 43 | + } |
| 44 | + if err := c.Close(); err != nil { |
| 45 | + klog.Errorf("error closing connection: %v", err) |
| 46 | + } |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + if p.ForceHost == "" && !p.isAllowed(req.URL.Host) { |
| 51 | + klog.Infof("host %s is not allowed", req.URL.Host) |
| 52 | + |
| 53 | + response := &http.Response{ |
| 54 | + StatusCode: http.StatusForbidden, |
| 55 | + ProtoMajor: 1, |
| 56 | + ProtoMinor: 1, |
| 57 | + } |
| 58 | + if err := response.Write(c); err != nil { |
| 59 | + klog.Errorf("error writing response: %v", err) |
| 60 | + } |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + klog.Infof("handling CONNECT to %s", req.URL.Host) |
| 65 | + |
| 66 | + response := &http.Response{ |
| 67 | + StatusCode: 200, |
| 68 | + ProtoMajor: 1, |
| 69 | + ProtoMinor: 1, |
| 70 | + } |
| 71 | + if err := response.Write(c); err != nil { |
| 72 | + klog.Errorf("error writing response: %v", err) |
| 73 | + if err := c.Close(); err != nil { |
| 74 | + klog.Errorf("error closing connection: %v", err) |
| 75 | + } |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + destConn, err := net.DialTimeout("tcp", p.getHost(req), 30*time.Second) |
| 80 | + if err != nil { |
| 81 | + klog.Errorf("error dialing destination: %v", err) |
| 82 | + |
| 83 | + response := &http.Response{ |
| 84 | + StatusCode: http.StatusRequestTimeout, |
| 85 | + ProtoMajor: 1, |
| 86 | + ProtoMinor: 1, |
| 87 | + } |
| 88 | + if err := response.Write(c); err != nil { |
| 89 | + klog.Errorf("error writing response: %v", err) |
| 90 | + } |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + go transfer(destConn, c) |
| 95 | + go transfer(c, destConn) |
| 96 | +} |
| 97 | + |
| 98 | +func (p *Proxy) getHost(req *http.Request) string { |
| 99 | + if p.ForceHost != "" { |
| 100 | + return p.ForceHost |
| 101 | + } |
| 102 | + return req.URL.Host |
| 103 | +} |
| 104 | + |
| 105 | +func transfer(destination io.WriteCloser, source io.ReadCloser) { |
| 106 | + defer destination.Close() |
| 107 | + defer source.Close() |
| 108 | + _, err := io.Copy(destination, source) |
| 109 | + if err != nil { |
| 110 | + klog.Errorf("error copying data: %v", err) |
| 111 | + } |
| 112 | +} |
0 commit comments