jookies / jasmin

Jasmin - Open source SMS gateway
http://jasminsms.com
Other
1.04k stars 558 forks source link

HTTP API #1118

Open ggoggg opened 1 year ago

ggoggg commented 1 year ago

The message content is not encoded correctly when 'coding' is set to 8 in the HTTP request body.

Kisuke-CZE commented 1 year ago

Are you sure? In my case it seems to be working OK. Important is that coding=8 is not UTF-8 but UTF-16.

Here is an example bash script which can send non-ASCII messages over Jasmin (it presumes your system encoding is UTF-8):

#!/bin/bash
USERNAME=jasmin_username
PASSWORD=jasmin_password
PHONENR_PATTERN='^([0-9\+]+).*'
JASMIN_URL='http://127.0.0.1:1401/send'

if [ -z "${1}" ]
then
  echo "There is no specified recipient."
  exit 1
fi

# Handling of multiple recipients - comma separated
IFS=',' read -r -a RECIPIENTS <<< "${1}"

# Read STDIN as message content - we are trying to be compatible with mail command here...
INPUT=$(< /dev/stdin)

# Let's decide if message is UCS2 or not
UNICODE=false
if [[ ${INPUT} = *[![:ascii:]]* ]]
then
  # echo "Contain Non-ASCII"
  # I do not know how to store UTF16 encoded string to variable for later usage, hence conversion into hex via xxd
  INPUT="$(echo -n "${INPUT}" | iconv -f UTF-8 -t UTF-16 | xxd -p)"
  UNICODE=true
fi

# Iterate over all recipients
for RECIPIENT in "${RECIPIENTS[@]}"
do
  # Just check if recipient contains only numbers. If there is any garbage behind number - strip it
  if [[ "${RECIPIENT}" =~ ${PHONENR_PATTERN} ]]
  then
    RECIPIENT="${BASH_REMATCH[1]}"
  else
    echo "Invalid recipient phone number: ${RECIPIENT}"
    exit 2
  fi

  case ${UNICODE} in
    (false)
      # Sending ASCII message using curl
      /usr/bin/curl \
        --data-urlencode "username=${USERNAME}" \
        --data-urlencode "password=${PASSWORD}" \
        --data-urlencode "to=${RECIPIENT}" \
        --data-urlencode "content=${INPUT}" \
        "${JASMIN_URL}" &>/dev/null
      ;;
    (true)
      # Sending UCS2 message using curl
      /usr/bin/curl \
        --data-urlencode "username=${USERNAME}" \
        --data-urlencode "password=${PASSWORD}" \
        --data-urlencode "to=${RECIPIENT}" \
        --data-urlencode "hex-content=${INPUT}" \
        --data-urlencode "coding=8" \
        "${JASMIN_URL}" &>/dev/null
      ;;
    (*)
      echo "Some malicious content found in UNICODE variable"
      exit 3
      ;;
  esac

done