Masturbation is Haram (prohibited), for Allah (Exalted be He) says:
And those who guard their chastity (i.e. private parts, from illegal sexual acts)
Except from their wives or (the slaves) that their right hands possess, - for then, they are free from blame;
In addition, this practice has harmful side effects.The doctor can use a needle to extract Maniy directly from your testes, for example. This is a permissible way instead of you having to masturbate. However, if it is the only way to get Maniy (Part No. 22; Page No. 58) and reach a treatment according to a specialist's medical report, then it is permissible in this case. On the other hand, you should try other forms of treatment, such as electric therapy, taking vitamins, and the like. May Allah heal you and keep you safe!May Allah grant us success. May peace and blessings be upon our Prophet Muhammad, his family, and Companions.