-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
75 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (C) 2024 The "MysteriumNetwork/openvpn-forwarder" Authors. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package proxy | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
var proxyRequestData = prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Name: "proxy_request_data", | ||
Help: "Proxy request data in bytes", | ||
}, []string{"request_type", "direction"}) | ||
|
||
func init() { | ||
prometheus.MustRegister(proxyRequestData) | ||
} | ||
|
||
// NewConn returns net.Conn wrapped with metrics. | ||
func NewConn(conn net.Conn) *Conn { | ||
return &Conn{ | ||
Conn: conn, | ||
} | ||
} | ||
|
||
// Conn wraps net.Conn with intercepts of read/write events. | ||
type Conn struct { | ||
net.Conn | ||
|
||
bytesSent int64 | ||
bytesReceived int64 | ||
} | ||
|
||
// Read bytes from net.Conn and count read bytes | ||
func (c *Conn) Read(b []byte) (n int, err error) { | ||
count, err := c.Conn.Read(b) | ||
|
||
proxyRequestData.MustCurryWith(prometheus.Labels{ | ||
"request_type": "HTTPS", | ||
}).WithLabelValues("received").Add(float64(count)) | ||
|
||
return count, err | ||
} | ||
|
||
// Write bytes to net.Conn and counts written bytes | ||
func (c *Conn) Write(b []byte) (n int, err error) { | ||
count, err := c.Conn.Write(b) | ||
|
||
proxyRequestData.MustCurryWith(prometheus.Labels{ | ||
"request_type": "HTTPS", | ||
}).WithLabelValues("sent").Add(float64(count)) | ||
|
||
return count, err | ||
} | ||
|
||
var _ net.Conn = (*Conn)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters