atrifat / nostr-filter-relay

A nostr relay docker image package which filter content based on content type (SFW/NSFW), user type, language, hate speech (toxic comment), sentiment, topic, and various rules.
MIT License
21 stars 1 forks source link

Error: connect ECONNREFUSED 127.0.0.1:7777 #25

Open kirpy0x opened 2 days ago

kirpy0x commented 2 days ago

The nostr-filter seems to be having issues communicating with strfry if I'm understanding this correctly. Getting Error: connect ECONNREFUSED 127.0.0.1:7777 in the nf.log when it first starts. In my config I set the detections to false and added hex pub/sec. In my reverse proxy logs, when initiating a query from nostr.info, Caddy says it got a "status":502 error.

Tried setting ENABLE_FORWARD_REQ_HEADERS=true Also tried images do this for me except 0.3.0 doesn't give the econnrefused error, however requests still get 502.

Logs:

==> /app/logs/strfry.log <==

==> /app/logs/nf.log <==

==> /app/logs/nmt.log <==

==> /app/logs/nf.log <==
{"msg":"process.env","DEFAULT_FILTER_HATE_SPEECH_TOXIC_MODE":"no","HATE_SPEECH_DETECTOR_TRUNCATE_LENGTH":"350","ENABLE_TOPIC_CLASSIFICATION":"false","NSFW_DETECTOR_TOKEN":"","HOSTNAME":"5a1853ac4c48","RELAYS_TO_PUBLISH":"ws://127.0.0.1:7777","SENTIMENT_ANALYSIS_ENDPOINT":"http://localhost:8084/predict","HATE_SPEECH_DETECTOR_TOKEN":"","NOSTR_MONITORING_BOT_PRIVATE_KEY":"******","ENABLE_HATE_SPEECH_DETECTION":"false","NSFW_DETECTOR_ENDPOINT":"http://localhost:8082/predict","YARN_VERSION":"1.22.22","LANGUAGE_DETECTOR_ENDPOINT":"http://localhost:5000/detect","MAX_WEBSOCKET_MESSAGE_PER_SECOND":"10","ENABLE_SENTIMENT_ANALYSIS":"false","PWD":"/app/nostr-filter","UPSTREAM_HTTP_URL":"http://127.0.0.1:7777","ENABLE_LANGUAGE_DETECTION":"false","TOPIC_CLASSIFICATION_TOKEN":"","NODE_ENV":"production","ENABLE_LEGACY_CLASSIFICATION_EVENT":"false","WHITELISTED_PUBKEYS":"","DEFAULT_FILTER_NSFW_CONFIDENCE":"75","HOME":"/home/node","USE_NIP_32_EVENT_FORMAT":"true","MAX_WEBSOCKET_PAYLOAD_SIZE":"1000000","HATE_SPEECH_DETECTOR_ENDPOINT":"http://localhost:8083/predict","MAX_WEBSOCKET_MESSAGE_PER_MINUTE":"1000","LISTEN_PORT":"7860","DELAYS_BEFORE_PUBLISHING_NOTES":"1000","DEFAULT_FILTER_TOPIC_MODE":"all","TOPIC_CLASSIFICATION_TRUNCATE_LENGTH":"350","ENABLE_NIP_32_CLASSIFICATION_EVENT":"true","SENTIMENT_ANALYSIS_TRUNCATE_LENGTH":"350","LANGUAGE_DETECTOR_TRUNCATE_LENGTH":"350","DEFAULT_FILTER_HATE_SPEECH_TOXIC_EVALUATION_MODE":"max","RATE_LIMIT_KEY":"\"IP\"","DEFAULT_FILTER_CONTENT_MODE":"sfw","DEFAULT_FILTER_HATE_SPEECH_TOXIC_CONFIDENCE":"75","TERM":"xterm","LANGUAGE_DETECTOR_TOKEN":"","RELAY_REQUEST_CONCURRENCY_LIMIT":"10","ENABLE_MQTT_PUBLISH":"false","DEFAULT_FILTER_USER_MODE":"all","UPSTREAM_WS_URL":"ws://127.0.0.1:7777","ENABLE_NSFW_CLASSIFICATION":"false","SHLVL":"1","DEFAULT_FILTER_SENTIMENT_MODE":"all","ENABLE_RATE_LIMIT":"false","RELAYS_SOURCE":"wss://relay.nostr.band","MQTT_BROKER_TO_PUBLISH":"","TOPIC_CLASSIFICATION_ENDPOINT":"http://localhost:8085/predict","DEFAULT_FILTER_TOPIC_CONFIDENCE":"35","DEFAULT_FILTER_LANGUAGE_CONFIDENCE":"15","ENABLE_FORWARD_REQ_HEADERS":"true","DEFAULT_FILTER_LANGUAGE_MODE":"all","SENTIMENT_ANALYSIS_TOKEN":"","PATH":"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","NOSTR_MONITORING_BOT_PUBLIC_KEY":"****","NODE_VERSION":"20.17.0","DEFAULT_FILTER_SENTIMENT_CONFIDENCE":"35","OLDPWD":"/app","_":"/usr/local/bin/node"}
{"msg":"configs","listenPort":7860,"upstreamHttpUrl":"http://127.0.0.1:7777","upstreamWsUrl":"ws://127.0.0.1:7777","contentFilters":[],"blockedIPAddresses":[]}
{"msg":"memoryUsage","currentTime":"2024-09-16T00:13:50.323Z","usedHeapSize":66,"totalHeapSize":100,"rssSize":117,"connectionCount":0}
{
  kinds: [ 1985 ],
  authors: [
    '*****'
  ],
  '#L': [
    'app.nfrelay.content-safety',
    'app.nfrelay.language',
    'app.nfrelay.toxicity',
    'app.nfrelay.sentiment',
    'app.nfrelay.topic'
  ]
}
node:events:497
      throw er; // Unhandled 'error' event
      ^

Error: connect ECONNREFUSED 127.0.0.1:7777
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1607:16)
Emitted 'error' event on WebSocket instance at:
    at emitErrorAndClose (/app/nostr-filter/node_modules/ws/lib/websocket.js:1041:13)
    at ClientRequest.<anonymous> (/app/nostr-filter/node_modules/ws/lib/websocket.js:881:5)
    at ClientRequest.emit (node:events:519:28)
    at emitErrorEvent (node:_http_client:108:11)
    at Socket.socketErrorListener (node:_http_client:511:5)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 7777
}
atrifat commented 2 days ago

HI @kirpy0x thank you for the report. Based on the log, it seems strfry were not started in docker container when you run that. Would you mind share more detail information?

If you have detail steps (including all cli command) then i will try to reproduce and take a look on this issue.

kirpy0x commented 2 days ago

main, dev, latest, and 0.4.0 With the above result. 0.3.0 didn't have the above error but still replied 502 when I tried to query it. Just tried 0.4.1 and same result

Originally I was trying docker compose but into troubleshooting I just started using docker run --init --env-file .env -p 7860:7860 -it ghcr.io/atrifat/nostr-filter-relay:v0.4.1 and trying different images.

I've also simplified the config to only modified the hex pub/sec and setting the detectors to false.

OS: Ubuntu 22.04.5 LTS - x86_64 - Docker version 27.2.1, build 9e34c9b

I haven't tried without docker. I can maybe try tomorrow.

kirpy0x commented 2 days ago

Looks like it may be a strfry thing. Tried running strfry manually without docker and got error - "strfry error: Unable to set NOFILES limit to 1000000, exceeds max of 524288"

Updated strfry.conf nofiles to 524288.

Ran again with no error, netstat shows it's listening on 7777, however still not able to query it. tried disabling firewall momentarily.

While I still have more troubleshooting to do on strfry's side it may be a good idea to add an option for the NOFILES property for strfry.conf in the .env.

Looks like others ran in to this in strfry's issues.

atrifat commented 1 day ago

Looks like it may be a strfry thing. Tried running strfry manually without docker and got error - "strfry error: Unable to set NOFILES limit to 1000000, exceeds max of 524288"

Updated strfry.conf nofiles to 524288.

Ran again with no error, netstat shows it's listening on 7777, however still not able to query it. tried disabling firewall momentarily.

While I still have more troubleshooting to do on strfry's side it may be a good idea to add an option for the NOFILES property for strfry.conf in the .env.

Looks like others ran in to this in strfry's issues.

I think, the issue might be related to OS file limit setting or Docker config that were set in your computer/devices.

Have you set/modify something that related to OS ulimit setting in your /etc/docker/daemon.json ?

Something like this probably?

{
  "default-ulimits": {
    "nofile": {
      "hard": 524288,
      "soft": 524288
    }
  }
}

If you have setting like this then it might prevent any container app set their own OS file descriptor limit. By default docker regular/fresh installation (Docker CLI, not Docker Desktop version) won't have anything in /etc/docker/daemon.json .

Also, do you modify something in /etc/security/limits.conf? What is the value of your /etc/security/limits.conf config?

I have already tested this using default docker installation and default config without any OS ulimit related changes in /etc/docker/daemon.json and /etc/security/limits.conf, it run properly in PC (Ubuntu 22.04 x86_64), Laptop (Ubuntu 24.04 x86_64), and VPS Server (Debian 12 x86_64). So, i think this is only happened in those cases when default config changes with some OS ulimit setting. strfry.conf that were included in nostr-filter-relay is based on default config of hoytech/strfry.

https://github.com/hoytech/strfry/blob/980725c2ae1073af58ee6e09090203b3564cccdc/strfry.conf#L50C15-L50C22

kirpy0x commented 1 day ago

Oh my gosh please forgive me. The reason it wasn't connecting was due to my mixing up my servers IPs! I'm so sorry!

The error listed still happens when the container starts up, however I'm not sure how much it's affecting the functionality. My Query and shows there shows there are equal amount of kind 1985's to 1's.

I can only think the strfry error was only occurring when not in Docker? I exec'd to the docker container and confirmed nofiles limit is set to 1000000 while it's seemingly working when in Docker.

daemon.json { "log-driver": "json-file", "log-opts": { "max-file": "10", "max-size": "100m" } } limits

#

* soft core 0

root hard core 100000

* hard rss 10000

@student hard nproc 20

@faculty soft nproc 20

@faculty hard nproc 50

ftp hard nproc 0

ftp - chroot /ftp

@student - maxlogins 4

The strfry connection error only happens at startup of the container.

atrifat commented 22 hours ago

Oh my gosh please forgive me. The reason it wasn't connecting was due to my mixing up my servers IPs! I'm so sorry!

The error listed still happens when the container starts up, however I'm not sure how much it's affecting the functionality. My Query and shows there shows there are equal amount of kind 1985's to 1's.

I can only think the strfry error was only occurring when not in Docker? I exec'd to the docker container and confirmed nofiles limit is set to 1000000 while it's seemingly working when in Docker.

daemon.json { "log-driver": "json-file", "log-opts": { "max-file": "10", "max-size": "100m" } } limits #

soft core 0 #root hard core 100000 # hard rss 10000 #@Student hard nproc 20 #@faculty soft nproc 20 #@faculty hard nproc 50 #ftp hard nproc 0 #ftp - chroot /ftp #@Student - maxlogins 4

The strfry connection error only happens at startup of the container.

Based on this, you only have problem when not running strfry in nostr-filter-relay docker?

If you want to run manually strfry on your machine, you can probably edit strfry.conf and change it according to your own machine limit.

I have tried this configuration .env in my PC and running properly. Can you please try this config and save as your .env without changing anything?

NODE_ENV=production

# ENV variable for nostr-monitoring-tool. Check atrifat/nostr-monitoring-tool github for more information.

# (Optional. Default: true) Set whether to publish NIP-32 classification event (kind: 1985)
ENABLE_NIP_32_CLASSIFICATION_EVENT=true
# (Optional. Default: true) (Deprecated) Set whether to publish legacy classification event (kind: 9978)
ENABLE_LEGACY_CLASSIFICATION_EVENT=true

ENABLE_NSFW_CLASSIFICATION=false
NSFW_DETECTOR_ENDPOINT=http://localhost:8082/predict
NSFW_DETECTOR_TOKEN=

ENABLE_LANGUAGE_DETECTION=false
LANGUAGE_DETECTOR_ENDPOINT=http://localhost:5000/detect
LANGUAGE_DETECTOR_TOKEN=
LANGUAGE_DETECTOR_TRUNCATE_LENGTH=350

ENABLE_HATE_SPEECH_DETECTION=false
# (Required if ENABLE_HATE_SPEECH_DETECTION == true) set this to your own hate-speech-detector-api instance (https://github.com/atrifat/hate-speech-detector-api)
HATE_SPEECH_DETECTOR_ENDPOINT=http://localhost:8083/predict
# (Optional) set this to your own hate-speech-detector-api api_key if required
HATE_SPEECH_DETECTOR_TOKEN=
# (Default: 350) Set to 0 if you don't want to truncate text, or set to any positive number to truncate the text characters
HATE_SPEECH_DETECTOR_TRUNCATE_LENGTH=350

ENABLE_SENTIMENT_ANALYSIS=false
# (Required if ENABLE_SENTIMENT_ANALYSIS == true) set this to your own sentiment-analysis-api instance (https://github.com/atrifat/sentiment-analysis-api)
SENTIMENT_ANALYSIS_ENDPOINT=http://localhost:8084/predict
# (Optional) set this to your own sentiment-analysis-api api_key if required
SENTIMENT_ANALYSIS_TOKEN=
# (Default: 350) Set to 0 if you don't want to truncate text, or set to any positive number to truncate the text characters
SENTIMENT_ANALYSIS_TRUNCATE_LENGTH=350

ENABLE_TOPIC_CLASSIFICATION=false
# (Required if ENABLE_TOPIC_CLASSIFICATION == true) set this to your own topic-classification-api instance (https://github.com/atrifat/topic-classification-api)
TOPIC_CLASSIFICATION_ENDPOINT=http://localhost:8085/predict
# (Optional) set this to your own topic-classification-api api_key if required
TOPIC_CLASSIFICATION_TOKEN=
# (Default: 350) Set to 0 if you don't want to truncate text, or set to any positive number to truncate the text characters
TOPIC_CLASSIFICATION_TRUNCATE_LENGTH=350

# (Required for classification filtering)
# Temporary uses "1" as secret key for debugging
NOSTR_MONITORING_BOT_PRIVATE_KEY=1
RELAYS_SOURCE=wss://relay.nostr.band
RELAYS_TO_PUBLISH=ws://127.0.0.1:7777
DELAYS_BEFORE_PUBLISHING_NOTES=1000
ENABLE_MQTT_PUBLISH=false
MQTT_BROKER_TO_PUBLISH=

# ENV variable for nostr-filter. Check atrifat/nostr-filter for more information.
UPSTREAM_HTTP_URL=http://127.0.0.1:7777
UPSTREAM_WS_URL=ws://127.0.0.1:7777
# (Required for classification filtering)
# 79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 is the public key of "1"
NOSTR_MONITORING_BOT_PUBLIC_KEY=79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
# (Optional) if it is not empty, it will accept events only from whitelisted pubkey. It can be set with NOSTR_MONITORING_BOT_PUBLIC_KEY value to make nostr-filter as read-only relay.
WHITELISTED_PUBKEYS=
LISTEN_PORT=7860
# (Optional) Set true to enable forwarding of request headers to upstream server, useful if relays behind reverse proxy
ENABLE_FORWARD_REQ_HEADERS=true

# (Default: true) Use NIP-32 Event Format (kind: 1985) or Deprecated Legacy Format (kind: 9978). Legacy format will be fully replaced by NIP-32 event format in the future.
USE_NIP_32_EVENT_FORMAT=true

# Set maximum websocket server payload size (maximum allowed message size) in bytes
MAX_WEBSOCKET_PAYLOAD_SIZE=1000000

# Set maximum number of parallel concurrency limit when requesting classification events to relay
RELAY_REQUEST_CONCURRENCY_LIMIT=10

# Set true to enable rate limit for number of websocket message
ENABLE_RATE_LIMIT=false
# Set to "IP" to rate limit based on IP addresses otherwise using socketId
RATE_LIMIT_KEY="IP"
# Maximum number of websocket message (REQ, EVENT, etc) per second per IP/socketId
MAX_WEBSOCKET_MESSAGE_PER_SECOND=10
# Maximum number of websocket message (REQ, EVENT, etc) per minute per IP/socketId
MAX_WEBSOCKET_MESSAGE_PER_MINUTE=1000

# (Optional. Default: sfw. Options: all, sfw, partialsfw, and nsfw) Filter hate speech (toxic comment).
DEFAULT_FILTER_CONTENT_MODE=sfw
# (Optional. Default: 75, Options: 0-100) Default minimum probability/confidence score to determine the classification of nsfw content
DEFAULT_FILTER_NSFW_CONFIDENCE=75

# (Optional. Default: all. Multiple Options: all, or other language code)
DEFAULT_FILTER_LANGUAGE_MODE=all
# (Optional. Default: 15. Options: 0-100) Default minimum probability/confidence score to determine the classification of language
DEFAULT_FILTER_LANGUAGE_CONFIDENCE=15

# (Optional. Default: no. Options: all, no, yes) Filter hate speech (toxic comment). "all" will disable filtering, "no" will filter out any detected hate speech content, "yes" will select only detected hate speech content
DEFAULT_FILTER_HATE_SPEECH_TOXIC_MODE=no
# (Optional. Default: 75. Options: 0-100) Default minimum probability/confidence score to determine the classification of hate speech (toxic comment)
DEFAULT_FILTER_HATE_SPEECH_TOXIC_CONFIDENCE=75
# (Optional. Default: max. Options: max, sum) Methods to determine toxic content by using max value from all toxic classes score or sum value of all toxic classes score
DEFAULT_FILTER_HATE_SPEECH_TOXIC_EVALUATION_MODE=max

# (Optional. Default: all, Multiple Options: all,negative,neutral,positive) Multiple options separated by comma (eg: neutral,positive => filter to get both neutral and positive sentiment)
DEFAULT_FILTER_SENTIMENT_MODE=all
# (Optional. Default: 35, Options: 0-100) Default minimum probability/confidence score in percentage to determine the classification of sentiment
DEFAULT_FILTER_SENTIMENT_CONFIDENCE=35

# (Default: all, Multiple Options: list of valid topic in atrifat/nostr-filter-relay Github) Multiple options separated by comma (eg: life,music,sport,science_and_technology => filter to get life (short version of: diaries_and_life), music, sport, science_and_technology)
DEFAULT_FILTER_TOPIC_MODE=all
# (Default: 35, Options: 0-100) Default minimum probability/confidence score in percentage to determine the classification of topic
DEFAULT_FILTER_TOPIC_CONFIDENCE=35

# (Optional. Default: all. Options: all, nostr, activitypub) Filter user type. "nostr" for native nostr users and "activitypub" for activitypub users coming from bridge
DEFAULT_FILTER_USER_MODE=all

And run using docker:

docker run --init --rm --env-file .env -p 7860:7860 -it ghcr.io/atrifat/nostr-filter-relay:v0.4.1

Is the problem still occur?