Skip to content

Commit 03a5c49

Browse files
authored
forward: remove hosts entry feature (#20)
* remove hosts entry feature * remove host entry feature
1 parent 54099fc commit 03a5c49

File tree

2 files changed

+1
-66
lines changed

2 files changed

+1
-66
lines changed

docs/source/forward-proxy.rst

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,8 @@ For more information, see the `Caddy TLS configuration <https://caddyserver.com/
170170
Running the SCION HTTP Forward Proxy locally
171171
--------------------------------------------
172172
End users can run the SCION HTTP Forward Proxy locally by following the installation steps above.
173-
For smooth running experience, grant DAC capabilities to the binary:
174173

175-
.. code-block:: bash
176-
177-
sudo setcap cap_dac_override=+ep scion-caddy
178-
179-
If you do not want to grant those privileges, you can run the binary without them but you will have to manually add the following line to your ``/etc/hosts`` before running the SCION HTTP Forward Proxy:
174+
Add the following line on ``/etc/hosts`` before running the SCION HTTP Forward Proxy:
180175

181176
.. code-block:: bash
182177

forward/forwardproxy.go

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"net"
2525
"net/http"
2626
"net/textproto"
27-
"os"
2827
"strconv"
2928
"strings"
3029
"sync"
@@ -41,7 +40,6 @@ import (
4140
)
4241

4342
const (
44-
hostsFile = "/etc/hosts"
4543
hostsComment = " # Line added by the SCION HTTP Forward Proxy"
4644
hostName = "forward-proxy.scion"
4745
// TODO: make the address injectable via configuration
@@ -99,17 +97,11 @@ func (cp *CoreProxy) Initialize() error {
9997
cp.metricsHandler = panpolicy.NewMetricsHandler(cp.policyManager, cp.logger.With(zap.String("component", "metrics-handler")))
10098
cp.resolver = resolver.NewPANResolver(cp.logger.With(zap.String("component", "resolver")), cp.resolveTimeout)
10199

102-
if err := cp.addHostsEntry(); err != nil {
103-
cp.logger.Warn("Failed to add entry to /etc/hosts file", zap.Error(err))
104-
}
105100
return nil
106101
}
107102

108103
// Cleanup cleans up the core proxy logic.
109104
func (cp *CoreProxy) Cleanup() error {
110-
if err := cp.removeHostsEntry(); err != nil {
111-
cp.logger.Warn("Failed to remove entry from /etc/hosts file", zap.Error(err))
112-
}
113105
return cp.policyManager.Stop()
114106
}
115107

@@ -174,58 +166,6 @@ func (cp *CoreProxy) HandleTunnelRequest(w http.ResponseWriter, r *http.Request)
174166
return cp.forwardRequest(w, r, dialer)
175167
}
176168

177-
func (cp *CoreProxy) addHostsEntry() error {
178-
content, err := os.ReadFile(hostsFile)
179-
if err != nil {
180-
return fmt.Errorf("failed to read hosts file: %w", err)
181-
}
182-
183-
lines := strings.Split(string(content), "\n")
184-
for _, line := range lines {
185-
if !strings.HasPrefix(strings.TrimSpace(line), "#") && strings.Contains(line, hostName) {
186-
cp.logger.Debug("Entry for host name already exists", zap.String("entry", line))
187-
return nil
188-
}
189-
}
190-
191-
file, err := os.OpenFile(hostsFile, os.O_APPEND|os.O_WRONLY, 0644)
192-
if err != nil {
193-
return fmt.Errorf("failed to open hosts file for writing: %w", err)
194-
}
195-
defer file.Close()
196-
197-
entry := hostsComment + "\n" + hostsEntry + "\n"
198-
if _, err := file.WriteString(entry); err != nil {
199-
return fmt.Errorf("failed to write to hosts file: %w", err)
200-
}
201-
202-
cp.logger.Info("Added entry to hosts file", zap.String("entry", hostsEntry))
203-
return nil
204-
}
205-
206-
func (cp *CoreProxy) removeHostsEntry() error {
207-
content, err := os.ReadFile(hostsFile)
208-
if err != nil {
209-
return fmt.Errorf("failed to read hosts file: %w", err)
210-
}
211-
212-
var newLines []string
213-
lines := strings.Split(string(content), "\n")
214-
for _, line := range lines {
215-
if !strings.Contains(line, hostsEntry) && !strings.Contains(line, hostsComment) {
216-
newLines = append(newLines, line)
217-
}
218-
}
219-
220-
err = os.WriteFile(hostsFile, []byte(strings.Join(newLines, "\n")), 0644)
221-
if err != nil {
222-
return fmt.Errorf("failed to write to hosts file: %w", err)
223-
}
224-
225-
cp.logger.Info("Removed entry from hosts file", zap.String("entry", hostsEntry))
226-
return nil
227-
}
228-
229169
func (cp *CoreProxy) parseCookieFromProxyAuth(w http.ResponseWriter, r *http.Request) error {
230170
// the path policy cookie is passed in the proxy-authorization header as the cookie
231171
username, cookie, err := proxyBasicAuth(r)

0 commit comments

Comments
 (0)